Use:
#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.
Answer from nims on Stack OverflowUse:
#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.
Videos
Good day,
So I created a structure Position with two int members.
struct Position {
int x;
int y;
};
typedef struct Position Position;
int main() {
Position *current = (Position*)malloc(sizeof(Position));
...
}If I want to create an array of it, should I just multiply it to how many elements I want to use?Does pointer arithmetic will move me to the next element struct every time I add 1 to the pointer?Why when I try to reallocate it by multiplying to higher number, it returns invalid pointer even though I just passed the pointer coming from malloc(). Thanks for answering and have a nice day.
Hi all - bit of help needed. I have two structs in C++.
The first structure needs to contain an array of the second type, but I cannot work out how to initialise this.
typedef struct
{
String something;
int somethingElse;
} harryType;
typedef struct
{
String Name;
harryType harrys[ ];
} fredType;
fredType freds[]={"bill",{"blah",3}, {"bleg",10}, "george", {"yuck",4}, {"meh",7} };
is what I tried - but it doesnt like the harrys[ ].
What is the solution please? I guess the memory needs allocating dynamically? Perhaps a list?
Cheers, Jim
Hello,
First of all, please don't tell me to search. I have searched and none of the answers have been able to help me solve my problem.
I am trying to make an array of structs. Here's my code:
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
struct NameValue {
char *name;
char *value;
};
void myfuction(){
int numvals; //the # of positions in the array
//code here that calculates and sets numvals
struct NameValue *NameValuearray = (struct NameValue*)calloc(numvals,sizeof(*NameValuearray));
int structpos;
char tempname[300];
char tempvalue[300];
for(structpos = 0; structpos < numvals; structpos++){
//code here that sets tempname and tempvalue. This is different every iteration
struct NameValue thisnamevalue;
thisnamevalue.name = tempname;
thisnamevalue.value = tempvalue;
NameValuearray[structpos] = thisnamevalue;
struct NameValue asdf = NameValuearray[0];
}
}I have verified that at the end of each iteration of the for loop, thisnamevalue does contain the correct information. I want the array to hold a copy of thisnamevalue after every iteration. So NameValuearray[0] contains thisnamevalue as it was at the end of the first iteration, NameValuearray[1] contains thisnamevalue as it was at the end of the second iteration, and so on. In this functionality, once NameValuearray[0] is set, it is not changed.
struct NameValue asdf = NameValuearray[0];
printf("%s\n", asdf.name);^ This code should then print the same thing numval times. Instead, each time it runs, it prints the 'name' of thisnamevalue as it was when it was last placed in the array. So running
NameValuearray[3] = thisnmevalue
should not change what the abouve two lines of code print, but it does. Does anybody know how to fix this?