Simple data structures

suggest change

A structure packages related data so that it can be handled as one object.

Declaring a simple struct that holds two int members:

struct point 
{
    int x;
    int y; 
};

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

Defining and using structs:

struct point p;    // declare p as a point struct
p.x = 5;           // assign members of p
p.y = 3;

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

struct point p = {5, 3};

Structs may also be initialized with designated initializers.

Members are also read with the . operator:

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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents