1

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.

Zoltán Csáti
  • 679
  • 5
  • 17

2 Answers2

0

This:

struct bin *binpointer = (struct bin*)calloc(N, sizeof(struct bin));

Should be:

(*myDynamicMembersBinArray).binpointer = calloc(N, sizeof(struct bin));

Note I got rid of the cast. See this answer for why (keeping in mind that in C++, you do need the cast. But it is wrong in C and can hide errors).

Community
  • 1
  • 1
JohnH
  • 2,713
  • 12
  • 21
  • `*myDynamicMembersBinArray` needs to be in parentheses to work correct and casting the return value of `malloc` is not wrong in C, it is only not good to do this. – mch May 12 '15 at 20:27
0

you did not declare myDynamicMembersBinArray:
struct dynamicbin* myDynamicMembersBinArray = calloc(1, sizeof(struct dynamicbin));

struct bin *binpointer = (struct bin*)calloc(N, sizeof(struct bin)); should be
(*myDynamicMembersBinArray).binPointer = calloc(N, sizeof(struct bin));
(the parentheses around *myDynamicMembersBinArray are important) or better
myDynamicMembersBinArray->binPointer = calloc(N, sizeof(struct bin));

and last but not least
printf("%f\n", (*(*myDynamicMembersBinArray).binPointer+i).size); should be
printf("%f\n", *((*myDynamicMembersBinArray).binPointer+i).size);
(note the * moved outside the of the parentheses) or much better
printf("%f\n", myDynamicMembersBinArray->binPointer[i].size);

mch
  • 9,424
  • 2
  • 28
  • 42
  • Thank you for your answers. I created myDynamicMembersBinArray, but forgot to paste it into the code here. After I have corrected the other two things, error messages occur, namely: in case of "myDynamicMembersBinArray->binpointer = calloc(N, sizeof(struct bin));" a value of type void* cannot be assigned to an entity of type bin. in case of "printf("%f\n", *((*myDynamicMembersBinArray).binPointer+i).size);" expression must have class type. As I mentioned, I use Visual C++. – Zoltán Csáti May 13 '15 at 04:11
  • I realized that the first issue was about the C++ development environment. I ran it on tcc, and it became OK. But for the printf section tcc said: lvalue expected. – Zoltán Csáti May 13 '15 at 04:16
  • It works if I write: printf("%f\n", (*(*myDynamicMembersBinArray).binPointer).size); – Zoltán Csáti May 13 '15 at 04:22
  • `printf("%f\n", (*(*myDynamicMembersBinArray).binPointer).size);` will always print the one with the index 0, not with the index `i`. this will work: https://ideone.com/x9VrtD – mch May 13 '15 at 07:38
  • My last question is: what is the case when `myDynamicMembersBinArray` is allocated to be of size `M`, i.e.: `const int M = 4; struct dynamicbin* myDynamicMembersBinArray = calloc(M, sizeof(struct dynamicbin));` In this case `for (int j=0; jbinPointer[i].size); }` won't work. Somehow, I have to step the pointer `myDynamicMembersBinArray`. – Zoltán Csáti May 13 '15 at 08:52
  • I tried: `printf("%f\n", myDynamicMembersBinArray[j]->binPointer[i].size);`, but it does not work. – Zoltán Csáti May 13 '15 at 09:01
  • you should use `printf("%f\n", myDynamicMembersBinArray[j].binPointer[i].size);` to get the `j`'th element of `myDynamicMembersBinArray`. – mch May 13 '15 at 10:48
  • All right. So the idea behind your solution is the following: first, allocate the outer structure and then all the elements of the inner structure array. Thank you again, it was very helpful! – Zoltán Csáti May 13 '15 at 11:28