array of structures within a struct

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2006
Posts: 6
Reputation: lalalu is an unknown quantity at this point 
Solved Threads: 0
lalalu lalalu is offline Offline
Newbie Poster

array of structures within a struct

 
0
  #1
Oct 9th, 2006
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?

  1. typedef struct status *StatusPointer;
  2.  
  3. struct status {
  4. int item;
  5. }array[MAX_NUM];
  6.  
  7. typedef struct allStatus *AllStatusPointer;
  8.  
  9. struct allStatus {
  10. StatusPointer array[MAX_NUM];
  11. };
if thats fine, how do i call the value of any item in the struct?

  1.  
  2. AllStatusPointer group1;
  3. group1 = (AllStatusPointer)malloc (sizeof (struct allStatus));
  4. //group1 != NULL
  5.  
  6. int james =group1->array[0]->item;
should this works?
Im using C
Last edited by lalalu; Oct 9th, 2006 at 6:12 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 65
Reputation: GloriousEremite will become famous soon enough GloriousEremite will become famous soon enough 
Solved Threads: 14
GloriousEremite GloriousEremite is offline Offline
Junior Poster in Training

Re: array of structures within a struct

 
0
  #2
Oct 9th, 2006
The first part looks ok to me, but for the second part you also need to allocate space for each pointer in the array
"What are the roots that clutch, what branches grow
out of this stony rubbish?"
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,508
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: array of structures within a struct

 
0
  #3
Oct 9th, 2006
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]
Last edited by Ancient Dragon; Oct 9th, 2006 at 6:40 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 6
Reputation: lalalu is an unknown quantity at this point 
Solved Threads: 0
lalalu lalalu is offline Offline
Newbie Poster

Re: array of structures within a struct

 
0
  #4
Oct 9th, 2006
Originally Posted by Ancient Dragon View Post
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)?

  1. int i = 0;
  2. for (i = 0; i < MAX_NUM; i++) { //MAX_NUM == array size
  3. array[i] = (StatusPointer) malloc (sizeof (struct status));
  4. }
like this?

and also, should i put it before group1 mem allocation, or after ?
Last edited by lalalu; Oct 9th, 2006 at 7:37 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 16
Reputation: peterbyrne is an unknown quantity at this point 
Solved Threads: 1
peterbyrne peterbyrne is offline Offline
Newbie Poster

Re: array of structures within a struct

 
0
  #5
Oct 9th, 2006
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[i] = (StatusPointer)malloc(sizeof(struct status));
group1->array[i]->item = i;
}
int james =group1->array[0]->item;
for (int i = 0; i < MAX_NUM; i++)
{
free(group1->array[i]);
group1->array[i] = NULL;
}
free(group1);
group1 = NULL;
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,508
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: array of structures within a struct

 
0
  #6
Oct 9th, 2006
Originally Posted by lalalu View Post
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)?

  1. int i = 0;
  2. for (i = 0; i < MAX_NUM; i++) { //MAX_NUM == array size
  3. array[i] = (StatusPointer) malloc (sizeof (struct status));
  4. }
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.
  1. struct allStatus {
  2. struct status array[MAX_NUM];
  3. };


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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 16
Reputation: peterbyrne is an unknown quantity at this point 
Solved Threads: 1
peterbyrne peterbyrne is offline Offline
Newbie Poster

Re: array of structures within a struct

 
0
  #7
Oct 9th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 16
Reputation: peterbyrne is an unknown quantity at this point 
Solved Threads: 1
peterbyrne peterbyrne is offline Offline
Newbie Poster

Re: array of structures within a struct

 
0
  #8
Oct 9th, 2006
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;
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 6
Reputation: lalalu is an unknown quantity at this point 
Solved Threads: 0
lalalu lalalu is offline Offline
Newbie Poster

Re: array of structures within a struct

 
0
  #9
Oct 9th, 2006
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
Last edited by lalalu; Oct 9th, 2006 at 9:20 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC