How do you make an array of structs in C? - Stack Overflow
Can someone explain how does array of structures work?
ARRAY of STRUCTS vs STRUCT of ARRAYS
[Syntax Question] Accessing a nested structure in an array of structures
ptr->animal.commonName is syntactically the same as (*ptr).animal.commonName. That's how the -> operator is defined to work. This makes sense why it works, you're deref'ing a pointer to get the underlying Pet structure, and accessing a field in that structure. In C pointers to arrays are pointers to their first element so deref'ing it directly gets you the first element.
ptr[1]->animal.commonName is syntactically the same as *( *(ptr+1) ).animal.commonName. This doesn't make sense; the array access already dereferences the pointer, then you try again with the -> operator. If you had an array of pointers to Pet this would make sense but you don't. If your compiler isn't issuing a fatal error on this you need to fix that.
ptr[0].petName works because you removed the second deref from above. It's the same as *(ptr+0).petName. It's also the exact same as ptr->petName. Do you see why?
ptr[1].petName is reading out of the array bounds. ptr originally pointed at a two-element array then you advanced it on line 35 so now index 1 is out of bounds.
ptr[-1].petName is perfectly fine because of line 35. After 35 ptr points to the second element of the array, so index -1 is the first element. Same logic goes for ptr[0].petName.
how do I refer to other members of the array of structures and also their nested values without using the accumulator operand on the pointer?
I'm not sure what you mean by 'accumulator operand', but the way to do it is using the array access syntax.
More on reddit.comWhat is an array of structure in C?
Why do we use array of structure in C programming?
What is the default value in a structure array?
Videos
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.
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.
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.