C Programming Arrays Of Structures

Last modified by Microchip on 2023/11/09 09:06

Syntax

If typeName or structName has already been defined:

typeName arrName[n];
           -or-
struct structName arrName[n];

Example

1 typedef struct
2 {
3  float re;    
4  float im;    
5 } complex;       
6 ...
7 complex a[3]; 

Initializing Arrays of Structures at Declaration

Syntax

If typeName or structName has already been defined:

typeName arrName[n] = {{list1},...,{listn}};
           -or-
struct structName arrName[n] = {{list1},...,{listn}};

Example

1 typedef struct
2 {
3  float re;    
4  float im;    
5 } complex;       
6 ...
7 complex a[3] = {{1.2, 2.5}, {3.9, 6.5}, {7.1, 8.4}}; 

Using Arrays of Structures

Syntax

If arrName has already been defined:

arrName[n].memberName

Example: Definitions

1 typedef struct
2 {
3  float re;    
4  float im;    
5 } complex;       
6 ...
7 complex a[3]; 

Example: Usage

1 int main(void)
2 {
3   a[0].re = 1.25;  
4  a[0].im = 2.50;   
5  ...
6