简体   繁体   中英

Adding elements to Struct Array

struct GENERATIONS
{
char generation[MAX_ROWS][MAX_COLS];
int hasCycle;
};

typedef struct GENERATIONS Generation;

I have an array of type struct:

Generation generations[MAX_GENERATIONS];

I declare a Generation variable like this:

Generation *currentGeneration = NULL;
currentGeneration = (Generation *) malloc(sizeof(Generation));

and attempt to add a generation to an array of generations: numGenerations is set to 0 then incremented via a loop.

copyGeneration(currentGeneration);
generations[numGenerations] = currentGeneration;

Yet each time, I get the error incompatible types when assigning to type 'Generation' from type 'struct Generation *. I understand this has to do with pointers which I do not understand but need.

Why is it that when I declare the array as:

Generation *generations[MAX_GENERATIONS];

Everything suddenly works?

Each currentGeneration is a pointer to a Generation . Yet when you declare an array Generation generations[MAX_GENERATIONS] it expects each index to be a Generation , not a pointer to one. But when you declare the array as Generation *generations[MAX_GENERATIONS] it expects each index to be a pointer to a Generation , which is what you are assigning to each index.

The error is telling you exactly what's wrong. Your variable currentGeneration is of type "pointer to Generation", and your variable generations is of type "array of Generation". You can't assign a pointer to Generation to an index of an array of Generation--you can only assign a Generation.

When you declare the array as Generation *generations[MAX_GENERATIONS] , everything works because you're assigning a pointer to Generation to an index of an array of pointers to Generation.

To solve this problem, you can proceed in a different way. What you can do is this

#define MAX_GENERATIONS 1024 // you can take some other value too
#include <stdio.h>
#include <stdlib.h>
static int count = 0

Generation** push(Generation** generations, Generation obj){
 count++;
 if (count == MAX_GENERATIONS){
   printf("Maximum limit reached\n");
   return generations;

 if ( count == 1 )
   generations = (Generation**)malloc(sizeof(Generation*) * count);
 else
   generations = (Generation**)realloc(generations, sizeof(Generation*) * count);

 generations[count - 1] = (Generation*)malloc(sizeof(Generation));
 generations[count - 1] = obj;

 return generations;
}

int main(){
  Generation** generations = NULL;
  Generation currentGeneration;
  // Scan the the elements into currentGeneration
  generations = push(generations, currentGeneration); // You can use it in a loop
}

currentGeneration is a Generation * , not a Generation .

You need an array of Generation * to hold it, not an array of Generation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM