C Language Simple data structures


Example

Structure data types are useful way to package related data and have them behave like a single variable.

Declaring a simple struct that holds two int members:

struct point 
{
    int x;
    int y; 
};

x and y are called the members (or fields) of point struct.

Defining and using structs:

struct point p;    // declare p as a point struct
p.x = 5;           // assign p member variables
p.y = 3;

Structs can be initialized at definition. The above is equivalent to:

struct point p = {5, 3};

Structs may also be initialized using designated initializers.

Accessing fields is also done using the . operator

printf("point is (x = %d, y = %d)", p.x, p.y);