Academic Program

Arrays of Structures

Arrays of Structures

Syntax

If typeName or structName has already been defined:

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

Example

1
2
3
4
5
6
7
typedef struct
{
  float re;   
  float im;   
} complex;      
...
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
2
3
4
5
6
7
typedef struct
{
  float re;   
  float im;   
} complex;      
...
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
2
3
4
5
6
7
typedef struct
{
  float re;   
  float im;   
} complex;      
...
complex a[3];

Example: Usage

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