Link to home
Start Free TrialLog in
Avatar of dastrw
dastrw

asked on

Dynamic Memory Alloc for struct in struct

I am trying to do dynamic memory allocation for a structure which contains arrays of structures.  I keep getting an 'assignment type mismatch' error when I try to compile the code.

Basically I'm trying to store data for a number of tests performed at a number of different sites.  If I can get this simpler code to work I will add other layers of structs to hold the data points and test conditions.

Here is the code so far:

#include <stdio.h>
main()
{
  int i, j, sz, NO_OF_SITES, NO_OF_TESTS;
  struct testStruct {
    int i_test;
    int j_test;
  };
  struct siteStruct {
    int i_site;
    int j_site;
    int k_site;
    float x_site;
    float y_site;
    struct testStruct *test;
  };

  struct siteStruct *site;
  struct testStruct *p_test;

  /* I read these values from a file so I
     don't know them at compile time */
  NO_OF_SITES = 3;
  NO_OF_TESTS = 5;

  sz = NO_OF_SITES * sizeof (struct siteStruct);
  site = (struct siteStruct *) malloc (sz);
  for (j=0; j<NO_OF_SITES; j++) {
    for (i=0; i<NO_OF_TESTS; i++) {
      sz = sizeof (struct testStruct);
      p_test = (struct testStruct *) malloc (sz);
      site[j].test[i] = p_test;
    }
  }
}

I get the error on this line:

      site[j].test[i] = p_test;

I get the same error with this code:

      site[j]->test[i] = p_test;

The compiler tells me that the thing on the left is a structure.  How do I make it look like a pointer to a structure?

What am I doing wrong?  Thanks.

Avatar of kotan
kotan
Flag of Malaysia image

The member test of siteStruct should be double pointer declaration. Because you need an array of pointer to testStruct

  struct siteStruct {
   int i_site;
   int j_site;
   int k_site;
   float x_site;
   float y_site;
   struct testStruct **test;
 };


Then the memory allocation for test,

  struct siteStruct *site;
 struct testStruct *p_test;

 /* I read these values from a file so I
    don't know them at compile time */
 NO_OF_SITES = 3;
 NO_OF_TESTS = 5;

 sz = NO_OF_SITES * sizeof (struct siteStruct);
 site = (struct siteStruct *) malloc (sz);
 for (j=0; j<NO_OF_SITES; j++) {
   sz = NO_OF_TESTS * sizeof(struct testStruct*);
   site[j].test = (struct testStruct**)malloc(sz);
   for (i=0; i<NO_OF_TESTS; i++) {
     sz = sizeof (struct testStruct);
     p_test = (struct testStruct *) malloc (sz);
     site[j].test[i] = p_test;
   }
 }
ASKER CERTIFIED SOLUTION
Avatar of kotan
kotan
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dastrw
dastrw

ASKER

Thanks.  I like your second solution best.  I should have realized that if I could allocate space for all my sites with one alloc, that I could do likewise with all of the tests per site.

Please answer this question to get the points.
dastrw

there is a button "Accept comment as answer" next to each comment. That's how you can accept a comment ...

=====
Werner