Solved

Dynamic Memory Alloc for struct in struct

Posted on 2002-07-01
4
340 Views
Last Modified: 2010-04-15
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.

0
Comment
Question by:dastrw
  • 2
4 Comments
 
LVL 6

Expert Comment

by:kotan
ID: 7123735
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;
   }
 }
0
 
LVL 6

Accepted Solution

by:
kotan earned 100 total points
ID: 7123737
Or

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

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

Author Comment

by:dastrw
ID: 7124978
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.
0
 
LVL 11

Expert Comment

by:griessh
ID: 7136131
dastrw

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

=====
Werner
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question