| | |
array of structures within a struct
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 6
Reputation:
Solved Threads: 0
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?
if thats fine, how do i call the value of any item in the struct?
should this works?
Im using C
C++ Syntax (Toggle Plain Text)
typedef struct status *StatusPointer; struct status { int item; }array[MAX_NUM]; typedef struct allStatus *AllStatusPointer; struct allStatus { StatusPointer array[MAX_NUM]; };
C++ Syntax (Toggle Plain Text)
AllStatusPointer group1; group1 = (AllStatusPointer)malloc (sizeof (struct allStatus)); //group1 != NULL int james =group1->array[0]->item;
Im using C
Last edited by lalalu; Oct 9th, 2006 at 6:12 pm.
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]
[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.
•
•
Join Date: Oct 2006
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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]
C++ Syntax (Toggle Plain Text)
int i = 0; for (i = 0; i < MAX_NUM; i++) { //MAX_NUM == array size array[i] = (StatusPointer) malloc (sizeof (struct status)); }
and also, should i put it before group1 mem allocation, or after ?
Last edited by lalalu; Oct 9th, 2006 at 7:37 pm.
•
•
Join Date: Oct 2006
Posts: 16
Reputation:
Solved Threads: 1
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;
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;
•
•
•
•
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)?
C++ Syntax (Toggle Plain Text)
int i = 0; for (i = 0; i < MAX_NUM; i++) { //MAX_NUM == array size array[i] = (StatusPointer) malloc (sizeof (struct status)); }
C++ Syntax (Toggle Plain Text)
struct allStatus { struct status array[MAX_NUM]; };
•
•
•
•
and also, should i put it before group1 mem allocation, or after ?
•
•
Join Date: Oct 2006
Posts: 16
Reputation:
Solved Threads: 1
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
Program
Header file
struct allStatus { status array[MAX_NUM]; };
C++ Syntax (Toggle Plain Text)
•
•
Join Date: Oct 2006
Posts: 16
Reputation:
Solved Threads: 1
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; }
•
•
Join Date: Oct 2006
Posts: 6
Reputation:
Solved Threads: 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
[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.
![]() |
Similar Threads
- Error with array structures :( (C++)
- C++ Array help needed (C++)
- Dynamic Array of Structures, Loop problem! (C++)
- struct Weaather (C++)
- Creating dynamic array structures (C++)
- Passing array of structures into a function (C++)
- How do I use an array of structures? (C++)
- dynamic array of structures problem (C++)
Other Threads in the C++ Forum
- Previous Thread: help need
- Next Thread: Re: Sound in C++
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg simple sorting string strings template text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






