How do you make an array of structs in C? - c

I'm trying to make an array of structs where each struct represents a celestial body.
I don't have that much experience with structs, which is why I decided to try to use them instead of a whole bunch of arrays. However, I keep on running into numerous different errors. I've tried to implement the techniques that I've seen on various threads and on StackOverflow (such as Array of structs in C and C - initialize array of structs), however not all of them were applicable.
Further information for those who have read this far: I don't need any of this to be dynamic, I know/define the size of everything beforehand. I also need this to be a global array as I'm accessing this in several different methods which have defined arguments (i.e. GLUT methods).
This is how I'm defining the struct 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'm defining before I define the interior of the struct, and one of those is the array of this struct (basically, if I'm being too unclear in my fogged speak, the line below is above the stuff above):
struct body bodies[n];
Just so you know, n is something that I've legitimately defined (i.e. #define n 1).
I use this array in several different methods, but the easiest and least space consuming one is a simplified form of my main. Here I initialize all of the variables in each of the structs, just to set the variables for certain before I modify 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 that I'm facing is nbody.c:32:13: error: array type has incomplete element type where line 32 is where I'm making the array of the structs.
One last clarification, by header I mean the space above int main(void) but in the same *.c file.

#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 fine. your question was not very clear by the way, so match the layout of your source code with the above.

Another way of initializing an array of structs is to initialize the array members explicitly. This approach is useful and simple if there aren't too many struct and array members.
Use the typedef specifier to avoid re-using the struct statement everytime you declare a struct variable:
typedef struct
{
double p[3];//position
double v[3];//velocity
double a[3];//acceleration
double radius;
double mass;
}Body;
Then declare your array of structs. Initialization of each element goes along with the declaration:
Body bodies[n] = {{{0,0,0}, {0,0,0}, {0,0,0}, 0, 1.0},
{{0,0,0}, {0,0,0}, {0,0,0}, 0, 1.0},
{{0,0,0}, {0,0,0}, {0,0,0}, 0, 1.0}};
To repeat, this is a rather simple and straightforward solution if you don't have too many array elements and large struct members and if you, as you stated, are not interested in a more dynamic approach. This approach can also be useful if the struct members are initialized with named enum-variables (and not just numbers like the example above) whereby it gives the code-reader a better overview of the purpose and function of a structure and its members in certain applications.

So to put it all together by 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);
}

I think you could write it that way too. I am also a student so I understand your struggle. A bit late response 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];

move
struct body bodies[n];
to after
struct body
{
double p[3];//position
double v[3];//velocity
double a[3];//acceleration
double radius;
double mass;
};
Rest all looks fine.

Solution using pointers:
#include<stdio.h>
#include<stdlib.h>
#define n 3
struct body
{
double p[3];//position
double v[3];//velocity
double a[3];//acceleration
double radius;
double *mass;
};
int main()
{
struct body *bodies = (struct body*)malloc(n*sizeof(struct body));
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;
}

That error means that the compiler is not able to find the definition of the type of your struct before the declaration of the array of structs, since you're saying you have the definition of the struct in a header file and the error is in nbody.c then you should check if you're including correctly the header file.
Check your #include's and make sure the definition of the struct is done before declaring any variable of that type.

You can do it in a same manner as you create the array of numbers but wrap the element's values in braces like this ->
struct Wrestler studs[count] = {
{"John", "Cena"},
{"The", "Undertaker"},
{"The", "Big Show"},
{"The", "Rock"},
{"Triple", "H"},
{"Scott", "Hall"},
{"Roman", "Reings"},
{"Dean", "Ambrose"}};
Here is full code
#include <stdio.h>
struct Wrestler
{
char firstName[20];
char secondName[20];
};
void pIntro(struct Wrestler *s)
{
printf("Hi, I am %s %s.\n", s->firstName, s->secondName);
};
int main(int argc, char const *argv[])
{
#define count 8
struct Wrestler studs[count] = {
{"John", "Cena"},
{"The", "Undertaker"},
{"The", "Big Show"},
{"The", "Rock"},
{"Triple", "H"},
{"Scott", "Hall"},
{"Roman", "Reings"},
{"Dean", "Ambrose"}};
for (int i = 0; i < count; i++)
{
pIntro(&(studs[i]));
}
return 0;
}

Related

Structure in a Structure and Array

I have a few problems with the following assignments:
a)
Define a new data type tpos for storing the position of a solid in the two dimensional plane in
single precision. Afterwards define another new data type tsolid that can store the weight of
a solid in double precision and its position in the two dimensional plane. Use tpos for defining
the position component of tsolid.
b)
Define an array of length two of type tsolid. The positions and weights of both solids (array
elements) shall now be read from the keyboard by using the scanf function. For checking
correctness of your program, display the array content on the screen.
So far i have:
#include <stdio.h>
#include <stdlib.h>
struct tpos
{
float xy
};
struct tsolid
{
struct tpos;
double m;
};
int main()
{
struct tsolid array[2];
return 0;
}
How do I Proceed now? Already tried a few things, but sadly they didnt work out. The text basically says i have to save 1 solid in 1 array element, right? But how can i save pos x , pos y, and the weight m all in 1 array element and then print them all at the same time? Do i have to use a pointer ?
First of all, if you need to store "two dimensional plane", you should use two elements inside struct.
Other thing is that you don't access struct elements by saying struct tpos; - you should use tpos xy.
Repaired code:
#include <stdio.h>
#include <stdlib.h>
struct tpos
{
float x;
float y;
};
struct tsolid
{
tpos xy;
double m;
};
int main()
{
tsolid array[2];
return 0;
}
When it comes to scanf and displaying content you need to decide yourself on the "output layout".
And you access certain parts of the "element" by using
array[i].m;
array[i].xy.x;
array[i].xy.y;
tpos needs two members, one for the each coordinate.
tsolid needs a name for the tpos member.
Then write a loop that asks for each value, puts them in the array, and another loop that prints the values from the array.
#include <stdio.h>
struct tpos
{
float x, y;
};
struct tsolid
{
struct tpos position;
double mass;
};
int main()
{
struct tsolid array[2];
for (int i = 0; i < 2; i++) {
printf("Enter x, y, and mass for object %d:\n", i+1);
scanf("%f %f %lf", &array[i].position.x, &array[i].position.y, &array[i].mass);
}
printf("You entered:\n");
for (int i = 0; i < 2; i++) {
scanf("x = %f, y = %f mass = %lf\n", array[i].position.x, array[i].position.y, array[i].mass);
}
return 0;
}

C function signature problems

I'm working on a C assignment for school and the assignment asks us to use this specific function signature that is causing errors for my compiler.
I'm getting an error from line 38 (the smallest function signature) in vector_test.c, the error reads ""," expected (got "*")". This is my first time working in C so I think I must be doing something wrong in regards to how I have setup the typedef in types.h or something along those lines, just not exactly sure and thought I'd get some extra opinions. Anything you could point out that I'm doing wrong here would be helpful, thank you!
Here's the code:
vector_test.c
#include "types.h"
int smallest(const Vector3t, int);
void main (int argc, char *argv[]) {
unsigned int N = 0;
printf ("Number of elements ===>");
scanf ("%u", &N);
struct Vector3t points[N];
for (int i = 0; i < N; i++)
{
scanf("%d,%d,%d", &points[i].x, &points[i].y, &points[i].z);
}
for (int p = 0; p < N; p++)
{
printf("%i", points[p].x);
printf("%i", points[p].y);
printf("%i\n\n", points[p].z);
}
int result = smallest(points, N);
printf("%s", "The point closest to the origin is (");
printf("%i", points[result].x);
printf("%s", ", ");
printf("%i", points[result].y);
printf("%s", ", ");
printf("%i", points[result].z);
printf("%s", ")");
}
int smallest(const Vector3t* pts, int n)
{
int shortest = 99999999;
int shortIndex = -1;
for (int i = 0; i < n; i++)
{
int distance = pts[i].x + pts[i].y + pts[i].z;
if (distance < shortest)
{
shortest = distance;
shortIndex = i;
}
}
return shortIndex;
}
types.h
#ifndef types_H
#define types_H
struct vec3 {
int x;
int y;
int z;
};
typedef struct Vector3t {
int x;
int y;
int z;
} vec3;
#endif
Here's the assignment instructions for this specific part so you can see what I'm trying to do:
1. Create a header file called “types.h” (with a macro guard)
a. In this header file, create a struct type called vec3 with int types: x, y, and z
b. Create a typedef to the struct vec3 above and call it Vector3t
2. Create a C source file called “vector_test.c”
a. This file should include “types.h” and any other C system includes you may need
b. Create a main function that does the following:
i. Prompts the user “Number of elements ===> “
ii. Read an unsigned int from the user – this variable will be referred to as N
iii. Create an array of Vector3t of size N and call it points
iv. Read in triples of int (comma separated) from the user to populate the entire array
v. Using the function signature: int smallest(const Vector3t* pts, int n), implement a
function that returns the index of the point that is the closest to the point (0, 0, 0)
vi. Call this function and store the index into an int called result
vii. Print out to the user “The point closest to the origin is (<x>, <y>, <z>)” where <x>, <y>, and <z>
correspond to the values of points[result]
viii. Free all allocated data
Your function forward declaration is:
int smallest(const Vector3t, int);
While your function definition says:
int smallest(const Vector3t* pts, int n)
In your forward declaration you're saying that you're passing in the struct as a parameter, while in your definition you're saying that it's taking a pointer to the struct. These are incompatible signatures.
You get it wrong in the first steps:
Create a header file called "types.h" (with a macro guard)
In this header file, create a struct type called vec3 with int types: x, y, and z
Create a typedef to the struct vec3 above and call it Vector3t
For first,
struct vec3 {
int x;
int y;
int z;
};
is correct. Then to define the typedef, you first give the the actual type, then the type alias:
typedef struct vec3 Vector3t;
Alternatively, these 2 can be combined into one typedef:
typedef struct vec3 {
int x;
int y;
int z;
} Vector3t;
Also the declaration of smallest doesn't match the definition (shouldn't they look alike?) and the return type of main must be int.

Structs and passing values to them

If i had an array such as int numbers[5] i could assign values to it with numbers[0] = 1 or numbers[3] = 4. Then if i had a struct such as
struct structName
{
int number0;
int number1;
int number2;
};
is there any way to do something like the following (note this is not working code)
int main(void)
{
struct structName name; //how could i declare this to do the following
for(int i = 0; i < 2; i++)
{
name[i] = i; //maybe name.[i]
}
}
so is there a way to write name[ variable ] = someNumber to assign someNumber to say number0 (if variable was 0) or number2 (if variable was 2). ive been looking for days and cant find anything that does this. (maybe i just don't know what to look for)
is there any way to do something like the following
No, there's no way to access the fields of the structure by index. You use the names of the fields instead:
struct structName name;
name.number0 = someNumber;
name.number1 = someOtherNumber;
If you want to access the values by index, use an array instead, even if it's embedded in the structure:
struct structName
{
int numbers[3];
// other fields here
};
Then you can say:
struct structName name;
for (int i = 0; i <= 2, i++) {
name.numbers[i] = i;
}
You could write a function which uses a switch statement that allows you to access fields by index. Something like:
#include<stdio.h>
struct structName{
int number0;
int number1;
int number2;
};
void assign(struct structName * name, int i, int j){
switch(i){
case 0:
name->number0 = j;
break;
case 1:
name->number1 = j;
break;
case 2:
name->number2 = j;
break;
}
}
int main(void){
int i;
struct structName name;
for(i = 0; i <= 2; i++){
assign(&name,i,i);
}
//test:
printf("%d\n",name.number0);
printf("%d\n",name.number1);
printf("%d\n",name.number2);
return 0;
}
(which prints 0,1,2 as expected).
Needless to say, there isn't much point in doing this (as opposed to just having a field which is an array) unless the struct in question is already defined as part of an API or already part of a code base which isn't easily refactored.
Yes, with some weird and inadvisable memory manipulation. You're much better off using an array.
struct structName
{
int numbers[3];
};
int main(void)
{
struct structName name;
for(int i = 0; i <= 2; i++)
{
name.numbers[i] = i;
}
}
Also note that you had some syntax errors in your for loop and an off-by-one error.
Macros with arguments should work
#define name(x) x
So name(1) would become 1. name(2) would become 2 and so on.
In C, there is no spoon.
struct structName name;
int *idx = &name; // First we need a memory address to the struct
for (int i = 0; i < sizeof(name) / sizeof(*idx); ++i) {
// idx[i] == name.numberX
idx[i] = i;
}
Now, if you check the values of name.number0, name.number1, name.number2 you will see they contain the correct values.
This is not a very good way of doing things with structs, but I felt compelled to answer after the top response claims it is impossible.

Casting data array to compatible struct

I'm in a situation where my code receives data from somewhere beyond my control in the form of a long list of floats.
These numbers get distributed to various functions
void myfunc(struct floatstruct* fs);
that take structs of the following form:
struct floatstruct
{
float a;
float b;
float c;
};
You get the idea.
I was wondering if there is a way to safely cast the array of floats to floatstruct to pass the data directly on to myfunc. I can add alignment attributes to floatstruct if necessary.
Example of desired behaviour:
struct mystruct1
{
float a;
float b;
float c;
};
struct mystruct2
{
float x;
float y;
};
extern void myfunc1(mystruct1*);
extern void myfunc2(mystruct2*);
void process_data(float* numbers)
{
myfunc1((struct mystruct1*)numbers);
myfunc2((struct mystruct2*)(numbers + 3));
}
The ideal solution is surely to change the system. But I'm looking for solutions within the given parameters.
Here's what I would do, given your peculiar requirements: ( I say this because 3 new floats here or there will make literally no noticeable difference unless you plan to use this on an arduino or phone or if you plan on having like tens of thousands...)
Anyways:
struct floatstruct
{
float (*a[3]);
};
{
int i;
struct floatstruct aStruct;
struct floatstruct bStruct;
float *num = numbers;
for (i = 0; i < 6; i++) {
if (i < 3)
aStruct.a[i] = num;
else
bStruct.a[i-3] = num;
num++;
}
myfunc1(&aStruct);
myfunc2(&bStruct);
}
Union could be what you need:
#pragma pack(sizeof(float))
struct mystruct1
{
float a;
float b;
float c;
};
struct mystruct2
{
float x;
float y;
};
#pragma pack()
typedef union
{
mystruct1 struct1;
mystruct2 struct2;
}structsUnion;
void myfunc1(structsUnion* values)
{
values->struct1.a; // to access members
}
void myfunc2(structsUnion* values)
{
values->struct2.x; // to access members
}
void process_data(float* numbers)
{
myfunc1((structsUnion*)(numbers));
myfunc2((structsUnion*)(numbers));
}

Failing to pass variable of struct-array as function parameter

I am fairly new to the concept of structs in C, and have run into a problem. I have looked for any similar problems posted here, but cannot find any. What I am trying to do is pass a variable in an array of structs as a parameter in a function, as so:
struct Student
{
float average;
int ID;
int grades[5];
char firstName[20], lastName[20];
};
void main(void)
{
struct Student sTable[10];
float maxAverage(float sTable[].average)
{
int i;
float max = 0;
for(i = 0;i < 10; ++i)
{
if(sTable[i].value > max)
{
max += sTable[i].value;
}
}
return max;
}
printf("%f",maxAverage(sTable[].average));
}
There are a few problems here.
You cannot nest functions inside other functions like you can in languages like Java, C#, Python.
You are passing the struct array incorrectly.
Your main declaration is incorrect.
You want code something like this:
struct Student
{
float average;
int ID;
int grades[5];
char firstName[20], lastName[20];
};
float maxAverage(struct Student sTable[])
{
int i;
float max = 0;
for(i = 0;i < 10; ++i)
{
if(sTable[i].value > max)
{
max += sTable[i].average;
}
}
return max;
}
int main(void)
{
struct Student sTable[10];
//initialize sTable
printf("%f", maxAverage(sTable));
return 0;
}
Note that you are missing initialization of sTable.
What's more you really ought to pass the length of the array as a parameter to maxAverage. That will then allow you more flexibility to use arrays of any length.
First of all, you can't declare or define a function inside another function, so move maxAverage outside of main (which, incidentally, should return int, not void). Secondly, given an array of structs, you can't just automatically create an array of the values from a certain field of the structs; if you want to, you would have to manually declare a separate array of (in this case) floats and then copy over the average fields one by one. A simpler way would be to simply pass sTable itself to maxAverage and have maxAverage only operate on the average fields of the structs in the array.

Categories

Resources