K
Koster
Sorry for the re-post, but my previous question was left unanswered.
I have a question about the appropriateness of calloc. Consider an array of
pointers to structs which need to be allocated space on the heap, for example:
typedef struct myStruct *PMYSTRUCT;
struct myStruct
{
int i;
int j;
...etc...
}
PMYSTRUCT myArray[10];
While I know I can point and allocate each element manually my looping mallocs,
I wonder if calloc will do what I want. Will executing "myArray = calloc(10,
sizeof(myStruct));" allocate 10 myStructs somewhere on the heap and put pointers
to each one in the myArray array, or will it allocate the myStructs and point
myArray[0] to it, thereby making myArray[1] point somewhere inside the 1st
myStruct and messing up everything?
I hope that's clear enough,
Koster.
I have a question about the appropriateness of calloc. Consider an array of
pointers to structs which need to be allocated space on the heap, for example:
typedef struct myStruct *PMYSTRUCT;
struct myStruct
{
int i;
int j;
...etc...
}
PMYSTRUCT myArray[10];
While I know I can point and allocate each element manually my looping mallocs,
I wonder if calloc will do what I want. Will executing "myArray = calloc(10,
sizeof(myStruct));" allocate 10 myStructs somewhere on the heap and put pointers
to each one in the myArray array, or will it allocate the myStructs and point
myArray[0] to it, thereby making myArray[1] point somewhere inside the 1st
myStruct and messing up everything?
I hope that's clear enough,
Koster.