items is a pointer, so you must dereference it accordingly.Code:test.items.numbers = (int *)malloc(sizeof(int) *n);
Code:test.items->numbers = (int *)malloc(sizeof(int) *n);
items is a pointer, so you must dereference it accordingly.Code:test.items.numbers = (int *)malloc(sizeof(int) *n);
Code:test.items->numbers = (int *)malloc(sizeof(int) *n);
bit∙hub [bit-huhb] n. A source and destination for information.
Couple of things: there is no purpose to casting malloc in C:
Also: you have not initialized N or n to any value!Code:test.items = (child_structure *)malloc(sizeof(child_structure) *N);
Next: What bithub says is true UNLESS you actually want "items" to be a " dynamically allocated array of ints". Look:
One struct, with a member, items, which is supposed to be an array. ButCode:parent_structure test;
That's not an array! You want:Code:test.items = malloc(sizeof(child_structure) *N); test.items.numbers = (int *)malloc(sizeof(int) *n);
Get it?Code:for (i=0; i<N; i++) { test.items[i].numbers = malloc(sizeof(int) *n); }
You can do it the other way, but then you cannot access elements of test.items via array[subscripting] -- you must use pointer arithmetic on each element. Also, you would have to malloc test.items N*n*sizeof(int).
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Thank you everyone for the swift and very helpful input!
MK27 your advice on using the for loop made perfect sense! I was already thinking that some kind of loop was needed, but wasn't sure...but since I plan to access the arrays using subscripting, that was the info I needed.
Quick question, you guys said that in C you don't need to cast the mallocs...but is there any harm in doing it anyway?
The reason I'm asking is because I'll be implementing a lot of these kinds of structures in my programming assignment, and my professor requires us to submit our assignment by turning in the source code. He said that the TA's will be strictly using g++ to compile all our programs and grade them. If it doesn't compile then we lose points.
The thing is, if I don't typecast the mallocs, then it'll compile just fine in gcc, but it won't compile in g++. If I typecast them, then it compiles in g++.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
You will want to stick with one way, since C does not require it and it might just hide a grave error, but in C++, it is required, and the pitfall that exists in C does not exist either, so it is safe.
Since you are a C++ programmer, though, I suppose I should advise you to cast the return and compile with a C++ compiler.