How do you create an array of structures in C?

I am trying to create an array of structures where each structure represents a celestial body for a problem that I am working on in my class. I don’t have such a lot of experience with structures, so I decided to try to use them instead of a number of arrays, however, I constantly encounter many different errors, although I tried to implement the methods that I saw in different streams and in stackoverflow (for example, in an Array of structures in C and C - to initialize an array of structs ), however, not all of them are applicable, so I could not completely copy the way to do this. Before I show you what I'm trying to do, I won’t answer the comments / questions / answers for several hours, because I need to go to bed since I wake up too long, I really regret it, but I'm really tired after a busy day and have been working on this problem for several hours.

Additional information for those who read this: I do not need this to be dynamic, I know / determine the size of everything in advance. I also need it to be a global array (gasp GLOBAL VARIABLES ), since I access it in several ways that have certain arguments (i.e. GLUT methods).

This is how I define the structure in my header:

struct body { double p[3];//position double v[3];//velocity double a[3];//acceleration double radius; double mass; }; 

I have a list of other global variables that I define before I define the interior of the structure, and one of them is an array of this structure (basically, if I'm too unclear in my foggy conversation, the line is lower than above):

 struct body bodies[n]; 

Just so you know, n is what I definitely defined (i.e. #define n 1 ).

I use this array in several ways, but the simplest and least expensive space is the simplified form of my main, where I initialize all the variables in each of the structures, just to set the variables for certain ones before changing them in some way:

  int a, b; for(a = 0; a < n; a++) { for(b = 0; b < 3; b++) { bodies[a].p[b] = 0; bodies[a].v[b] = 0; bodies[a].a[b] = 0; } bodies[a].mass = 0; bodies[a].radius = 1.0; } 

The current error I encountered is nbody.c:32:13: error: array type has incomplete element type , where line 32 is the place where I create the array of structures.

Thank you for any help you deign to give, I promise that I will get back to you very recently, in 12 hours.

The last refinement by title means the space above int main(void) , but in the same * .c file.

+69
c arrays struct
May 6 '12 at 4:35
source share
5 answers
 #include<stdio.h> #define n 3 struct body { double p[3];//position double v[3];//velocity double a[3];//acceleration double radius; double mass; }; struct body bodies[n]; int main() { int a, b; for(a = 0; a < n; a++) { for(b = 0; b < 3; b++) { bodies[a].p[b] = 0; bodies[a].v[b] = 0; bodies[a].a[b] = 0; } bodies[a].mass = 0; bodies[a].radius = 1.0; } return 0; } 

this works great. your question was not very clear, so compare the layout of the source code with the one above.

+72
May 6 '12 at 4:47
source share

move

 struct body bodies[n]; 

after

 struct body { double p[3];//position double v[3];//velocity double a[3];//acceleration double radius; double mass; }; 

Everything else looks fine.

+7
May 6 '12 at 4:41
source share

I think you could write this too. I am also a student, so I understand your struggle. A bit late answer, but ok.

 #include<stdio.h> #define n 3 struct { double p[3];//position double v[3];//velocity double a[3];//acceleration double radius; double mass; }bodies[n]; 
+7
Jun 08 '15 at 18:48
source share

So, to put it all together using malloc() :

 int main(int argc, char** argv) { typedef struct{ char* firstName; char* lastName; int day; int month; int year; }STUDENT; int numStudents=3; int x; STUDENT* students = malloc(numStudents * sizeof *students); for (x = 0; x < numStudents; x++){ students[x].firstName=(char*)malloc(sizeof(char*)); scanf("%s",students[x].firstName); students[x].lastName=(char*)malloc(sizeof(char*)); scanf("%s",students[x].lastName); scanf("%d",&students[x].day); scanf("%d",&students[x].month); scanf("%d",&students[x].year); } for (x = 0; x < numStudents; x++) printf("first name: %s, surname: %s, day: %d, month: %d, year: %d\n",students[x].firstName,students[x].lastName,students[x].day,students[x].month,students[x].year); return (EXIT_SUCCESS); } 
+3
Jul 02 '14 at 22:20
source share

This error means that the compiler cannot find the definition of the type of your structure before declaring the array of structures, because you say that you have a structure definition in the header file and the error is in nbody.c , then you should check the correct input of the header file. Check #include and make sure that the structure definition is complete before any variable of this type is declared.

+1
May 6 '12 at 4:50
source share



All Articles