im trying to create a structure that contains an array of structures but i keep getting error when compiling the code (corrupt stack). just to make sure, is my struct code correct?

typedef struct status *StatusPointer;

struct status { 
int item;
}array[MAX_NUM];

typedef struct allStatus *AllStatusPointer;

struct allStatus {
StatusPointer array[MAX_NUM];
};

if thats fine, how do i call the value of any item in the struct?

AllStatusPointer group1;
  group1 = (AllStatusPointer)malloc (sizeof (struct allStatus));
//group1 != NULL

 int james =group1->array[0]->item;

should this works?
Im using C

The first part looks ok to me, but for the second part you also need to allocate space for each pointer in the array

that is the right syntax, but it does not allocate enough memory because array inside the allStatus structure is an array of pointers and each of them must be malloc'ed as well.

[edit]Sorry Glorious -- your post was not there when I started writing the above[/edit]

that is the right syntax, but it does not allocate enough memory because array inside the allStatus structure is an array of pointers and each of them must be malloc'ed as well.

[edit]Sorry Glorious -- your post was not there when I started writing the above[/edit]

hmm, i dont really get it. so i have to malloc each of the array (ie using loop, allocate space for each structure in the array)?

int i = 0;
for (i = 0; i < MAX_NUM; i++) {      //MAX_NUM == array size
array[i] = (StatusPointer) malloc (sizeof (struct status));
}

like this?

and also, should i put it before group1 mem allocation, or after ?

Havent done any C for about 10 years but thought I'd have a quick look.
Your typedefs are fine - You dont need the word struct inside the sizeof operator although it doesn't do any harm. It will still work fine. What you have not done is allocate any memory for your status pointers. You have an array of 5 pointers to status objects but they could be pointing anywhere. You must allocate memory for each of these status pointers before you can use them. Dont forget to release the memory afterwards. Its been fun looking at this but also a reminder why I switched to C++ :-)
Try this:
AllStatusPointer group1 = (AllStatusPointer)malloc(sizeof(allStatus));
for (int i = 0; i < MAX_NUM; i++)
{
group1->array = (StatusPointer)malloc(sizeof(struct status));
group1->array->item = i;
}
int james =group1->array[0]->item;
for (int i = 0; i < MAX_NUM; i++)
{
free(group1->array);
group1->array = NULL;
}
free(group1);
group1 = NULL;

hmm, i dont really get it. so i have to malloc each of the array (ie using loop, allocate space for each structure in the array)?

int i = 0;
for (i = 0; i < MAX_NUM; i++) {      //MAX_NUM == array size
array[i] = (StatusPointer) malloc (sizeof (struct status));
}

Yes like that. Unless your assignment requires you to use pointers -- why use them? just statically allocate all those structures and it will cause fewer problems.

struct allStatus {
struct status  array[MAX_NUM];
};

and also, should i put it before group1 mem allocation, or after ?

memory must be allocated from the outermost structure to the innermost -- and free'ed in the opposite order.

Actually he has a good point. You are not in the situation where you have to dynamically assign memory as you have a constant size so there is in fact no need for pointers at all.
Header file

struct allStatus 
{
status array[MAX_NUM];
};

Program

Program

int _tmain(int argc, _TCHAR* argv[])
{
allStatus group1;
//Initialise to some values
for (int i=0; i < MAX_NUM; i++)
group1.array[0].item = i*5;
int jack = group1.array[0].item;
int jill = group1.array[1].item
return 0;
}

thanks guys. yea i think ill just statistically allocate those array

[qoute]Unless your assignment requires you to use pointers[/qoute]

no its doesnt thanks for pointing that out for me ;) cause theres a bunch of ADTs given for this task so things get a little messy

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.