I want to dynamically allocate structures within structures. I think I can do this. However when I want to perform operations with them, I get an access violation error under Visual C++. Perhaps there is something wrong with the initialization. By the way, how can I initialize myDynamicMembersBinArray
being dynamically allocated? I tried the following:
#include <stdio.h>
#include <stdlib.h>
int main()
{
/* ========== Define structures ========== */
/* Create just the identifier */
struct bin{
char *tag;
float size;
};
/* Declare another structure which will hold the bin structure */
struct dynamicbin{
struct bin *binPointer;
};
/* I use calloc so that zero-filling is done */
myDynamicMembersBinArray = (struct dynamicbin*)calloc(1, sizeof(struct dynamicbin));
struct bin *binpointer = (struct bin*)calloc(N, sizeof(struct bin));
/* This one fails */
for (int i=0; i<N; i++){
printf("%f\n", (*(*myDynamicMembersBinArray).binPointer+i).size);
}
Thank you in advance.