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 Overflow
Top answer
1 of 10
149

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.

2 of 10
28

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.

🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-array-of-structure
Array of Structures in C - GeeksforGeeks
An array of structures is simply an array where each element is a structure.
Published   October 21, 2025
Discussions

Creating an array of `structs`
I'm defining a struct and attempting to create an array filled with 89 of them. The method below compiles, and using keyBuffer[key].keyStruck or keyBuffer[key].channel etc, I can access correct values. key is an integer between 0 - 88. However, I suspect that the code isn't doing quite what ... More on forum.arduino.cc
🌐 forum.arduino.cc
13
0
December 15, 2020
Array of structs, quick like C
In Rust you have to put the variables and the name of the struct in each array element. In C you just put the literals. const v0: i8 = 0; const v1: i8 = 1; const v2: i8 = 2; const v3: i8 = 3; struct Arr { v: i8, … More on users.rust-lang.org
🌐 users.rust-lang.org
4
0
November 3, 2022
Initializing an array of structs
Hi there, I am looking for an easy way to initialize an array of structs (I do not want to type out, as it is pretty large). I isolated my problem to this code: #[derive(Debug)] struct TestStruct { a: i32, b: f… More on users.rust-lang.org
🌐 users.rust-lang.org
0
1
January 7, 2020
How to initialize an array of structs
I want to create an array of structs like shown below mutable struct mystruct a::Float32 b::Float32 c::UInt32 end mystruct() = mystruct(0,0,0) size = 3 a = Array{mystruct, 3}(undef, size, size, size) for i in eachindex(a) a[i] = mystruct() end Is there anyway to initialize the structs in the ... More on discourse.julialang.org
🌐 discourse.julialang.org
9
1
July 2, 2022

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 Overflow
🌐
Reddit
reddit.com › r/c_programming › can someone explain how does array of structures work?
r/C_Programming on Reddit: Can someone explain how does array of structures work?
January 15, 2023 -

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.

🌐
Wikipedia
en.wikipedia.org › wiki › AoS_and_SoA
AoS and SoA - Wikipedia
November 3, 2025 - In computing, an array of structures (AoS), structure of arrays (SoA) or array of structures of arrays (AoSoA) are contrasting ways to arrange a sequence of records in memory, with regard to interleaving, and are of interest in SIMD and SIMT programming.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › how-to-create-an-array-of-structs-in-c
How to Create an Array of Structs in C? - GeeksforGeeks
July 23, 2025 - To create an array of structs, we first need to define the struct type and then declare an array of that type using the below syntax.
Find elsewhere
🌐
Rust Programming Language
users.rust-lang.org › help
Initializing an array of structs - help - The Rust Programming Language Forum
January 7, 2020 - Hi there, I am looking for an easy way to initialize an array of structs (I do not want to type out, as it is pretty large). I isolated my problem to this code: #[derive(Debug)] struct TestStruct { a: i32, b: f32, c: char, d: bool } impl TestStruct { fn new() -> TestStruct { TestStruct {a: 1, b: 1.0, c: 'c', d: true} } } fn main() { let test_var = TestStruct::new(); let test_array = [TestStruct::new(); 20]; println!("test_var: {:#?}", test_var); ...
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_arrays_of_structures.htm
Array of Structures in C
In C programming, the struct keyword is used to define a derived data type. Once defined, you can declare an array of struct variables, just like an array of int, float or char types is declared.
🌐
Linux Hint
linuxhint.com › make-array-structs-c
How to Make an Array of Structs in C – Linux Hint
Array is a group of objects of same type. For example array of obj will be declared as struct abc obj[128]. Array of pointers to the structure objects will be as struct abc *ptr[128]. Both the array defined the 128 elements of structure objects and pointers.
🌐
Julia Programming Language
discourse.julialang.org › general usage › performance
How to initialize an array of structs - Performance - Julia Programming Language
July 2, 2022 - I want to create an array of structs like shown below mutable struct mystruct a::Float32 b::Float32 c::UInt32 end mystruct() = mystruct(0,0,0) size = 3 a = Array{mystruct, 3}(undef, size, size, size) fo…
🌐
Reddit
reddit.com › r/bigquery › array of structs vs struct of arrays
r/bigquery on Reddit: ARRAY of STRUCTS vs STRUCT of ARRAYS
September 8, 2024 -

Hi,

So I'm trying to learn the concept of STRUCTS, ARRAYS and how to use them.

I asked AI to create two sample tables: one using ARRAY of STRUCTS and another using STRUCT of ARRAYS.

This is what it created.

ARRAY of STRUCTS:

STRUCT of ARRAYS:

When it comes to this table- what is the 'correct' or 'optimal' way of storing this data?

I assume that if purchases is a collection of information about purchases (which product was bought, quantity and price) then we should use STRUCT of ARRAYS here, to 'group' data about purchases. Meaning, purchases would be the STRUCT and product_names, prices, quantities would be ARRAYS of data.

In such example- is it even logical to use ARRAY of STRUCTS? What if purchases was an ARRAY of STRUCTS inside. It doesn't really make sense to me here.

This is the data in both of them:

I guess ChatGPT brought up a good point:

"Each purchase is an independent entity with a set of associated attributes (e.g., product name, price, quantity). You are modeling multiple purchases, and each purchase should have its attributes grouped together. This is precisely what an Array of Structs does—it groups the attributes for each item in a neat, self-contained way.

If you use a Struct of Arrays, you are separating the attributes (product name, price, quantity) into distinct arrays, and you have to rely on index alignment to match them correctly. This is less intuitive for this case and can introduce complexities and potential errors in querying."

Top answer
1 of 5
3
Fun question. I think chatgpt is right. When it comes to extracting the data an array of structs will ensure a single struct within the array has the correct data sitting "on the same line". There could be use cases where null values in an array may be ignored or break iteration based processing if they aren't handled properly. It also has the potential for population to put things in the wrong order if it's not specified. No idea if one way is faster than another, though. Id feel doing a struct of arrays would have to be a lot faster for me to consider doing it that way, and if I did I'd try to keep it isolated from anything else.
2 of 5
3
ChatGPT is totally correct (in this case). An “array of structs” is an intrinsically useful structure. As an exercise, consider each struct as a form, like an employment application, and an array as a folder. Putting a bunch of structs into an array makes total sense, since you’re keeping each application intact, and grouping them. That’s why an “Array of Structs” is the most common user-enumerated data structure you’ll find. It’s even more useful when you consider that you can order the records within a given array, so you can enable logical assumptions about the first element being the earliest, smallest…whatever. A “Struct of Arrays”, though, is like taking each form, cutting it apart into its component fields…and then putting all the name slips into one container, all the address slips into a different container, and all the previous employment slips in a third. You’d need to have a whole separate system in place just to keep track of exactly which slips of paper came from which original forms. That’s what ChatGPT is referring to as “index alignment”. So one variation has clear implications that make it useful and common, while the other has implications that make it complicated, easy to screw up, and rare. That doesn’t mean that you’d never want to create a struct of arrays, but you’d want to have a very specific logical reason why that’s the most appropriate structure. Arrays of structs, however, are basically a “best-practice pattern” in BigQuery.
🌐
Ycpcs
ycpcs.github.io › cs101-fall2023 › lectures › lecture19.html
CS 101: Lecture 19: Arrays of structs (with functions)
Arrays of struct elements are treated the same as other arrays when being passed to functions, i.e. they are passed-by-reference (unlike a single struct element from the array which would be passed-by-value).
🌐
Bearblog
hwisnu.bearblog.dev › array-of-structs-and-struct-of-arrays
Array of Structs and Struct of Arrays
September 27, 2024 - Data structures play a crucial role in determining the performance and efficiency of algorithms. The Array of Structs (AoS) and Struct of Arrays (SoA) are two popular data structures used to represent complex data.
🌐
Scaler
scaler.com › home › topics › array of structure in c
Array of Structure in C - Scaler Topics
April 3, 2024 - It can be coupled with an array to create an array of structures. An array of structures is defined as a collection of structure variables that can, in turn, store various entities.
🌐
MathWorks
mathworks.com › matlab › language fundamentals › data types › structures
struct - Structure array - MATLAB
If any value input is an empty cell array, {}, then the output is an empty structure array. To specify a single empty field, use []. The struct function copies the properties of obj to the fields of a new scalar structure.
🌐
Llllllllll
llllllllll.github.io › principles-of-performance › arrays-and-structs.html
Arrays and Structs — principles-of-performance documentation
In order to access element \(n\) of an array at address \(p\) with elements of size \(s\) we just read the value at the address: \(p + sn\). Another common low-level data structure is called a “struct”, short for “structure”. Structures are a fixed length, ordered collection of potentially ...