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
🌐
WsCube Tech
wscubetech.com › resources › c-programming › array-of-structure
Array of Structures in C Programming (With Examples)
3 weeks ago - Learn in this tutorial about Array of Structures in C with examples. Understand how to initialize, access, and modify structures to manage data effectively in C.
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.

Discussions

ARRAY of STRUCTS vs STRUCT of ARRAYS
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. More on reddit.com
🌐 r/bigquery
10
12
September 8, 2024
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
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
Whats best? Array of Structs OR Struct of Arrays - Unity Engine - Unity Discussions
Hi Generally does it matter for the performance if I use Array of Structs OR Struct of Arrays ? /Thomas More on discussions.unity.com
🌐 discussions.unity.com
0
June 15, 2015
People also ask

Why do we use array of structure in C programming?
It helps store and process a group of similar data records (like multiple students, employees, etc.) in a structured way.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › array-of-structure
Array of Structures in C Programming (With Examples)
Can we use pointers with an array of structures?
Yes, you can use structure pointers to point to elements in the array.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › array-of-structure
Array of Structures in C Programming (With Examples)
What is an array of structure in C?
An array of structures in C is a collection of structure variables stored in an array. It allows you to store multiple records, such as student or employee details, using a single structure type.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › array-of-structure
Array of Structures in C Programming (With Examples)
🌐
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
🌐
Eecs280
eecs280.org
EECS 280
Computer science fundamentals, with programming in C++. Build a statistical analysis tool, an image processing program, a Euchre card game, a machine learning algorithm, and a text editor. Analyze and implement foundational data structures. Syllabus · Week 13: Week 13: Week 13: Week 13: Week 13: Week 13: base case · Project 5 is due Friday, April 10 @ 8pm. Office hours are cancelled on Sunday, April 5th for Easter.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › how-to-initialize-array-of-structs-in-c
How to Initialize Array of Structs in C? - GeeksforGeeks
July 23, 2025 - struct Str str[] = { {structure1_value1, structure1_value2}, {structure2_value1, structure2_value2}, {structure3_value1, structure3_value2} }; The below example demonstrates how we can initialize an array of structures in C.
Find elsewhere
🌐
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.
🌐
Arm Community
community.arm.com › support-forums › f › keil-forum › 20734 › array-of-struct-within-struct
Array of struct within struct - Keil forum - Support forums
Have a question? If you can, please take a moment to also see if there is a question that you are able to answer · These cookies are necessary for the website to function properly, and if you elect to block them, some parts of the site may not work
🌐
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.
🌐
HackerRank
hackerrank.com › domains › data-structures
Solve Programming Questions | HackerRank
Data Structures help in elegant representation of data for algorithms
🌐
Quora
quora.com › What-is-the-difference-between-a-struct-and-an-array-of-structs-in-the-C-programming-language
What is the difference between a 'struct' and an 'array of structs' in the C programming language? - Quora
Something of type struct house ... An array of struct house is a collection of struct house variables (instances), where each element of the array is effectively a separate struct house variable....
🌐
Godot Engine
docs.godotengine.org › en › stable › tutorials › scripting › gdscript › gdscript_basics.html
GDScript reference — Godot Engine (stable) documentation in English
GDScript is a high-level, object-oriented, imperative, and gradually typed programming language built for Godot. It uses an indentation-based syntax similar to languages like Python. Its goal is to...
🌐
Substack
thefulldatastack.substack.com › p › tech-deep-dive-pyarrow-compute-functions
Tech Deep Dive: PyArrow Compute Functions - by Hoyt Emerson
1 week ago - There are a few different buffers ... buffer to track variable-length values like lists and strings. Array: A structured view of multiple buffers....
🌐
Medium
medium.com › @DanielKrejci › array-of-structs-or-actors-in-blueprints-b647517cf2e4
Array of structs or actors in blueprints? | by Daniel K. | Medium
December 30, 2016 - I am not going talk about a structs in detail here. I am sure you can find better tutorials for that. An array of structs can be used to store multiple instances of same struct with different values.
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › everyday-types.html
TypeScript: Documentation - Everyday Types
Narrowing occurs when TypeScript can deduce a more specific type for a value based on the structure of the code. For example, TypeScript knows that only a string value will have a typeof value "string": ... Notice that in the else branch, we don’t need to do anything special - if x wasn’t a string[], then it must have been a string. Sometimes you’ll have a union where all the members have something in common. For example, both arrays and strings have a slice method.
🌐
Python Tutor
pythontutor.com › visualize.html
Python Tutor - Visualize Code Execution
It uses Valgrind to perform memory-safe run-time traversal of data structures, which lets it display data more accurately than gdb or printf debugging. For instance, it can precisely visualize critical concepts such as pointers, uninitialized memory, out-of-bounds errors, nested arrays/structs/unions, type punning, and bit manipulation.
🌐
Unity
discussions.unity.com › unity engine
Whats best? Array of Structs OR Struct of Arrays - Unity Engine - Unity Discussions
June 15, 2015 - Hi Generally does it matter for the performance if I use Array of Structs OR Struct of Arrays ? /Thomas
🌐
Pydantic
docs.pydantic.dev › latest › concepts › models
Models | Pydantic Docs
You can think of models as similar to structs in languages like C, or as the requirements of a single endpoint in an API.
🌐
GitConnected
levelup.gitconnected.com › cpu-driven-circular-buffer-7bc0cfe29cc3
CPU-Driven Circular Buffer. Did you know that you can delegate the… | by Ian Mihura | Feb, 2026 | Level Up Coding
February 23, 2026 - But we can force this map to something different. For example, we map several pages of our virtual memory to the same page in RAM.