Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamic Memory Alloc for struct in struct

Posted on 2002-07-01
4
Medium Priority
?
353 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

670 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