Solved

Dynamic Memory Alloc for struct in struct

Posted on 2002-07-01
4
335 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now