C Structure

Summary: in this tutorial, you will learn a new complex type called C structure. The C structure allows you to wrap related variables with different data types into a single entity that makes it easier to manipulate data in your program.

Introduction to C structure

c structureWhen you design a program, it is important to choose an optimal way to represent the data that the program processes. In simple cases, scalar variables and arrays are sufficient. However in practical programming context, you need a new form of variable that reflects the real world. For example, in your program, you may want to refer to an address that holds multiple fields including house number, street, zip code, state and country.

C provides a special kind of variable called structure. C structure allows you to wrap related variables that has different data types into a single variable. A structure can contain any valid data types such as  int, char, float, array, pointer or even other structures. Each variable in the structure is called structure member.

Defining structure

To define a structure, you use the struct keyword. The following illustrates the syntax of the structure definition:

The name of structure must follow the rules of variable name. The following example defines the address structure:

The address structure contains house_number as an unsigned int, street_name as a string, zip_code as an int and country as a string.

Declaring structure

The above example only defines the  address structure without creating any structure variable. There are two ways to declare a structure variable:

First, you can declare structure variables together with the structure definition as follows:

For example, to define home_address and business_address structures, you use the following code:

Second, you can declare the structure variable after you define the structure. The following is structure declaration syntax:

See the following example:

Complex structure

If a structure contains at least an array or a structure, it is called a complex structure. The structure that contains only scalar variables is called simple structure e.g., address structure is a simple structure.

We can define a complex structure called customer that contains address structure as follows:

Accessing structure member

To access a structure member, you use the dot operator ( .) between structure name and the member as follows:

The following example illustrates how to access street_name of the address structure :

If a structure contains another structure, you also use the dot operator ( .) to access the nested structure and its members.

Initializing structure variable

C treats a structure as a custom data type therefore you can initialize a structure like a variable. The following example demonstrates how to initialize product structure:

We defined the product structure; declared and initialized book structure variable.

Structure and pointer

A structure can contain pointers as structure members. You can create an invoice structure that contains  invoice_number as a pointer:

Shorthand structure with typedef keyword

To make the code clear, you can use typedef keyword to create a synonym for a structure. This is an example of using typedef keyword to define   address structure so that when you want to create a structure variable, you can omit the struct keyword

Copy a structure into another structure

One of major advantage of structure is that you can copy it with the assignment ( =) operator:

Notice that some old C compilers may not support structure assignment so you have to assign each structure member one by one.

Structure and the sizeof operator

The sizeof operator is used to get the size of any data type as well as a structure. Let’s take a look at following example:

You will never get the size of a structure exactly as you think it must be. The sizeof operator returns the size of a structure that is bigger than it is because the compiler need to pad structure members so that each member can be accessed faster without delay.

Example of using C structure

This example shows you how to use a structure to wrap student information and manipulate it by reading information to an array of student structures.

The following is the program’s output:

In this tutorial, you have learned how to use C structure to create new complex data types that wrap multiple related variables into one variable to make it more efficient to manipulate data.

  • Was this tutorial helpful ?
  • YesNo