Structure Definition – C Programming

In some programming contexts, you need to access multiple data types under a single name for easier data manipulation; for example you want to refer to address with multiple data like house number, street, zip code, country. C array is a derived data type allow you to define type of variables that can hold several data items of the same kind but structure is a user defined data type available in C programming, which allows you to combine data items of different kinds.

C supports structure which allows us to wrap one or more variables with different data types. A structure can contain any valid data types like int, char, float, arrays or even other structures. Each variable in structure is called a structure member.

To define a structure, you use struct keyword. Here is the common syntax of structure definition:

struct <structure-name>
{
<data-type> <variable-name>;
.
.
<data-type> <array-name> [<size>];
.
.
struct <other-struct-name>;
.
.
};

It should be noted that the name of structure follows the rule of variable name. Here is an example of defining address structure:

struct address
{
int house_number;
char street_name[50];
int zip_code;
char country[50];
};

The address structure contains house number as an integer, street name as a string, zip code as an integer and country as a string.