Solved

Dynamic Memory Alloc for struct in struct

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pointer in one class to member in another 6 132
Best UNIX-compatible free C compiler for Windows or Mac 6 289
IIS Log files on Exchange 2013 server 6 333
MySQL recovery 7 60
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…
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…
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.
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.

738 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