๐ŸŒ
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
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_arrays_of_structures.htm
Array of Structures in C
Its elements are "name"; marks in physics, chemistry and maths; and the "percentage". An array of three struct student types is declared and the first four elements are populated by user input, with a for loop.
Discussions

Can someone explain how does array of structures work?
You can do it a couple of different ways. You could pre-allocate an array of pointers and allocate each element. Or you can use the handy calloc() function. Or finally you can do as you said just multiply the size of the allocation by the number of elements. You can use the array subscript on the pointer current[i] and the compiler will do the arithmetic for you. calloc() is probably the best since it makes it explicit that you are allocating an array of items, it also will zero the memory for you. More on reddit.com
๐ŸŒ r/C_Programming
21
8
January 15, 2023
How do you make an array of structs in C? - Stack Overflow
To repeat, this is a rather simple ... 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 ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
c++ - Structure of Arrays vs Array of Structures - Stack Overflow
From some comments that I have read in here, it is preferable to have Structure of Arrays (SoA) over Array of Structures (AoS) for parallel implementations like CUDA. If that is true, can anyone ex... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
People also ask

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)
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)
What is the default value in a structure array?
If not initialized, structure members hold garbage (undefined) values.
๐ŸŒ
wscubetech.com
wscubetech.com โ€บ resources โ€บ c-programming โ€บ array-of-structure
Array of Structures in C Programming (With Examples)
๐ŸŒ
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.
๐ŸŒ
MathWorks
mathworks.com โ€บ matlab โ€บ language fundamentals โ€บ data types โ€บ structures
struct - Structure array - MATLAB
For any value that is a scalar cell array or an array of any other data type, struct inserts the contents of value in the relevant field for all elements of s. For example, s = struct('x',{'a','b'},'y','c') returns s(1).x = 'a', s(2).x = 'b', s(1).y = 'c', and s(2).y = 'c'.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ array-of-structures-vs-array-within-a-structure-in-c-and-cpp
Array of Structures vs Array within a Structure in C - GeeksforGeeks
July 15, 2025 - So, an array can comprise elements of this new data type. An array of structures finds its applications in grouping the records together and provides for fast access. Below is a demonstration of an array of structures. The array holds the details of the students in a class.
ways to arrange a sequence of records in memory
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 โ€ฆ Wikipedia
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ AoS_and_SoA
AoS and SoA - Wikipedia
November 3, 2025 - Array of structures (AoS) is the opposite (and more conventional) layout, in which data for different fields is interleaved. This is often more intuitive, and supported directly by most programming languages. For example, to store N points in 3D space using an array of structures:
Find elsewhere
๐ŸŒ
Javatpoint
javatpoint.com โ€บ array-of-structures-in-c
Array of Structures in C - javatpoint
Array of Structures in C with programming examples for beginners and professionals covering concepts, control statements. Let's see an example of structure with array in C.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ explain-the-array-of-structures-in-c-language
Explain the array of structures in C language
December 6, 2024 - Its elements are "name"; marks in physics, chemistry and maths; and the "percentage". An array of three struct student types is declared and the first four elements are populated by user input, with a for loop.
๐ŸŒ
ScienceDirect
sciencedirect.com โ€บ topics โ€บ computer-science โ€บ array-of-structure
Array Of Structure - an overview | ScienceDirect Topics
An array of structures (AoS) is a data layout in which multiple instances of a structure are allocated contiguously in memory, forming an array where each element is a complete structure with its own set of fields. 1 2 For example, a set of six-dimensional points can be stored as an array of ...
๐ŸŒ
MathWorks
mathworks.com โ€บ matlab โ€บ language fundamentals โ€บ data types โ€บ structures
Structure Arrays - MATLAB & Simulink
You can create a structure array having any size, as long as each structure in the array has the same fields. For example, add a second structure to patients having data about a second patient. Also, assign the original value of 127 to the billing field of the first structure.
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ array of structure in c
Array of Structure in C Explained with Example
January 4, 2026 - Instead of defining separate structure variables, you use one array and index through it. This structure also simplifies passing grouped data to functions. To initialize this array, use loops or direct assignment. For beginners, loops are usually easier and more flexible. ... Letโ€™s walk through a complete example using the student structure.
๐ŸŒ
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.

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.

๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ array of structures in c language explained with examples
Array Of Structures In C Language Explained With Examples
July 19, 2024 - The arrays of structures in C are especially useful when working with numerous records or entities that share the same set of attributes. General Example: Say we want to keep track of different products in a store.
Top answer
1 of 3
79

Choice of AoS versus SoA for optimum performance usually depends on access pattern. This is not just limited to CUDA however - similar considerations apply for any architecture where performance can be significantly affected by memory access pattern, e.g. where you have caches or where performance is better with contiguous memory access (e.g. coalesced memory accesses in CUDA).

E.g. for RGB pixels versus separate RGB planes:

struct {
    uint8_t r, g, b;
} AoS[N];

struct {
    uint8_t r[N];
    uint8_t g[N];
    uint8_t b[N];
} SoA;

If you are going to be accessing the R/G/B components of each pixel concurrently then AoS usually makes sense, since the successive reads of R, G, B components will be contiguous and usually contained within the same cache line. For CUDA this also means memory read/write coalescing.

However if you are going to process color planes separately then SoA might be preferred, e.g. if you want to scale all R values by some scale factor, then SoA means that all R components will be contiguous.

One further consideration is padding/alignment. For the RGB example above each element in an AoS layout is aligned to a multiple of 3 bytes, which may not be convenient for CUDA, SIMD, et al - in some cases perhaps even requiring padding within the struct to make alignment more convenient (e.g. add a dummy uint8_t element to ensure 4 byte alignment). In the SoA case however the planes are byte aligned which can be more convenient for certain algorithms/architectures.

For most image processing type applications the AoS scenario is much more common, but for other applications, or for specific image processing tasks this may not always be the case. When there is no obvious choice I would recommend AoS as the default choice.

See also this answer for more general discussion of AoS v SoA.

2 of 3
14

I just want to provide a simple example showing how a Struct of Arrays (SoA) performs better than an Array of Structs (AoS).

In the example, I'm considering three different versions of the same code:

  1. SoA (v1)
  2. Straight arrays (v2)
  3. AoS (v3)

In particular, version 2 considers the use of straight arrays. The timings of versions 2 and 3 are the same for this example and result to be better than version 1. I suspect that, in general, straight arrays could be preferable, although at the expense of readability, since, for example, loading from uniform cache could be enabled through const __restrict__ for this case.

#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include <stdio.h>

#include <thrust\device_vector.h>

#include "Utilities.cuh"
#include "TimingGPU.cuh"

#define BLOCKSIZE   1024

/******************************************/
/* CELL STRUCT LEADING TO ARRAY OF STRUCT */
/******************************************/
struct cellAoS {

    unsigned int    x1;
    unsigned int    x2;
    unsigned int    code;
    bool            done;

};

/*******************************************/
/* CELL STRUCT LEADING TO STRUCT OF ARRAYS */
/*******************************************/
struct cellSoA {

    unsigned int    *x1;
    unsigned int    *x2;
    unsigned int    *code;
    bool            *done;

};


/*******************************************/
/* KERNEL MANIPULATING THE ARRAY OF STRUCT */
/*******************************************/
__global__ void AoSvsSoA_v1(cellAoS *d_cells, const int N) {

    const int tid = threadIdx.x + blockIdx.x * blockDim.x;

    if (tid < N) {
        cellAoS tempCell = d_cells[tid];

        tempCell.x1 = tempCell.x1 + 10;
        tempCell.x2 = tempCell.x2 + 10;

        d_cells[tid] = tempCell;
    }

}

/******************************/
/* KERNEL MANIPULATING ARRAYS */
/******************************/
__global__ void AoSvsSoA_v2(unsigned int * __restrict__ d_x1, unsigned int * __restrict__ d_x2, const int N) {

    const int tid = threadIdx.x + blockIdx.x * blockDim.x;

    if (tid < N) {

        d_x1[tid] = d_x1[tid] + 10;
        d_x2[tid] = d_x2[tid] + 10;

    }

}

/********************************************/
/* KERNEL MANIPULATING THE STRUCT OF ARRAYS */
/********************************************/
__global__ void AoSvsSoA_v3(cellSoA cell, const int N) {

    const int tid = threadIdx.x + blockIdx.x * blockDim.x;

    if (tid < N) {

        cell.x1[tid] = cell.x1[tid] + 10;
        cell.x2[tid] = cell.x2[tid] + 10;

    }

}

/********/
/* MAIN */
/********/
int main() {

    const int N = 2048 * 2048 * 4;

    TimingGPU timerGPU;

    thrust::host_vector<cellAoS>    h_cells(N);
    thrust::device_vector<cellAoS>  d_cells(N);

    thrust::host_vector<unsigned int>   h_x1(N);
    thrust::host_vector<unsigned int>   h_x2(N);

    thrust::device_vector<unsigned int> d_x1(N);
    thrust::device_vector<unsigned int> d_x2(N);

    for (int k = 0; k < N; k++) {

        h_cells[k].x1 = k + 1;
        h_cells[k].x2 = k + 2;
        h_cells[k].code = k + 3;
        h_cells[k].done = true;

        h_x1[k] = k + 1;
        h_x2[k] = k + 2;

    }

    d_cells = h_cells;

    d_x1 = h_x1;
    d_x2 = h_x2;

    cellSoA cell;
    cell.x1 = thrust::raw_pointer_cast(d_x1.data());
    cell.x2 = thrust::raw_pointer_cast(d_x2.data());
    cell.code = NULL;
    cell.done = NULL;

    timerGPU.StartCounter();
    AoSvsSoA_v1 << <iDivUp(N, BLOCKSIZE), BLOCKSIZE >> >(thrust::raw_pointer_cast(d_cells.data()), N);
    gpuErrchk(cudaPeekAtLastError());
    gpuErrchk(cudaDeviceSynchronize());
    printf("Timing AoSvsSoA_v1 = %f\n", timerGPU.GetCounter());

    //timerGPU.StartCounter();
    //AoSvsSoA_v2 << <iDivUp(N, BLOCKSIZE), BLOCKSIZE >> >(thrust::raw_pointer_cast(d_x1.data()), thrust::raw_pointer_cast(d_x2.data()), N);
    //gpuErrchk(cudaPeekAtLastError());
    //gpuErrchk(cudaDeviceSynchronize());
    //printf("Timing AoSvsSoA_v2 = %f\n", timerGPU.GetCounter());

    timerGPU.StartCounter();
    AoSvsSoA_v3 << <iDivUp(N, BLOCKSIZE), BLOCKSIZE >> >(cell, N);
    gpuErrchk(cudaPeekAtLastError());
    gpuErrchk(cudaDeviceSynchronize());
    printf("Timing AoSvsSoA_v3 = %f\n", timerGPU.GetCounter());

    h_cells = d_cells;

    h_x1 = d_x1;
    h_x2 = d_x2;

    // --- Check results
    for (int k = 0; k < N; k++) {
        if (h_x1[k] != k + 11) {
            printf("h_x1[%i] not equal to %i\n", h_x1[k], k + 11);
            break;
        }
        if (h_x2[k] != k + 12) {
            printf("h_x2[%i] not equal to %i\n", h_x2[k], k + 12);
            break;
        }
        if (h_cells[k].x1 != k + 11) {
            printf("h_cells[%i].x1 not equal to %i\n", h_cells[k].x1, k + 11);
            break;
        }
        if (h_cells[k].x2 != k + 12) {
            printf("h_cells[%i].x2 not equal to %i\n", h_cells[k].x2, k + 12);
            break;
        }
    }

}

The following are the timings (runs performed on a GTX960):

Array of struct        9.1ms (v1 kernel)
Struct of arrays       3.3ms (v3 kernel)
Straight arrays        3.2ms (v2 kernel)
๐ŸŒ
Study.com
study.com โ€บ computer science courses โ€บ computer science 111: programming in c
Arrays of Structures in C Programming - Lesson | Study.com
December 12, 2018 - With structures, you can store ... The following example shows a structure called student that takes the roll number and name of a student as an input, then stores each record in an array st, which ......
๐ŸŒ
OverIQ
overiq.com โ€บ c-programming-101 โ€บ array-of-structures-in-c
Array of Structures in C - C Programming Tutorial - OverIQ.com
In lines 5-10, we have declared a structure called the student. In line 14, we have declared an array of structures of type struct student whose size is controlled by symbolic constant MAX.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ array-data-structure-guide
Array Data Structure - GeeksforGeeks
An array is a fundamental and linear data structure that stores items at contiguous locations. Note that in case of C/C++ and Java-Primitive-Arrays, actual elements are stored at contiguous locations. And in case of Python, JS, Java-Non-Primitive, references are stored at contiguous locations.
Published ย  March 3, 2026
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Array_(data_structure)
Array (data structure) - Wikipedia
November 15, 2025 - For example, an array of ten 32-bit (4-byte) integer variables, with indices 0 through 9, may be stored as ten words at memory addresses 2000, 2004, 2008, ..., 2036, (in hexadecimal: 0x7D0, 0x7D4, 0x7D8, ..., 0x7F4) so that the element with ...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ array of structure in c
Array of Structure in C - Scaler Topics
April 3, 2024 - So we don't need to take 10 different variables instead we could use array of structure student. Here, we will see an example where we can take input for the marks of many students in a class using an array of structures.