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
When 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:
1 | struct struct_name{ structure_member }; |
The name of structure must follow the rules of variable name. The following example defines the address
structure:
1 2 3 4 5 6 | struct address{ unsigned int house_number; char street_name[50]; int zip_code; char country[50]; }; |
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:
1 2 3 4 | struct struct_name { structure_member; ... } instance_1,instance_2 instance_n; |
For example, to define home_address and business_address structures, you use the following code:
1 2 3 4 5 6 | struct address{ unsigned int house_number; char street_name[50]; int zip_code; char country[50]; } home_address, business_address. |
Second, you can declare the structure variable after you define the structure. The following is structure declaration syntax:
1 | struct struct_name instance_1,instance_2, instance_n; |
See the following example:
1 | struct address home_address, business_address; |
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:
1 2 3 4 5 | struct customer{ char name[50]; structure address billing_addr; structure address shipping_addr; }; |
Accessing structure member
To access a structure member, you use the dot operator ( .
) between structure name and the member as follows:
1 | structure_name.structure_member |
The following example illustrates how to access street_name
of the address
structure :
1 2 | struct address billing_addr; billing_addr.country = "US"; |
If a structure contains another structure, you also use the dot operator ( .
) to access the nested structure and its members.
1 2 | struct customer jack; jack.billing_addr.country = "US"; |
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:
1 2 3 4 | struct product{ char name[50]; double price; } book = { "C programming language",40.5}; |
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:
1 2 3 4 5 6 7 | struct invoice{ char* invoice_number; char date[20]; }; struct address billing_addr; struct address *pa = &billing_addr; |
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
1 2 3 4 5 6 7 8 9 | typedef struct{ unsigned int house_number; char street_name[50]; int zip_code; char country[50]; } address; address billing_addr; address shipping_addr; |
Copy a structure into another structure
One of major advantage of structure is that you can copy it with the assignment ( =
) operator:
1 | struct_intance1 = struct_intance2 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <stdio.h> typedef struct __address{ int house_number;// 4 bytes char street[50]; // 50 bytes int zip_code; // 4 bytes char country[20];// 20 bytes } address;//78 bytes in total void main() { // it returns 80 bytes printf("size of address is %d bytes\n",sizeof(address)); } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <stdio.h> typedef struct _student{ char name[50]; unsigned int mark; } student; void print_list(student list[], int size); void read_list(student list[], int size); void main(){ const int size = 3; student list[size]; read_list(list,size); print_list(list,size); } void read_list(student list[], int size) { printf("Please enter the student information:\n"); for(int i = 0; i < size;i++){ printf("\nname:"); scanf("%S",&list[i].name); printf("\nmark:"); scanf("%U",&list[i].mark); } } void print_list(student list[], int size){ printf("Students' information:\n"); for(int i = 0; i < size;i++){ printf("\nname: %s, mark: %u",list[i].name,list[i].mark); } } |
The following is the program’s output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Please enter the student information: name:Jack mark:5 name:Anna mark:7 name:Harry mark:8 Students' information: name: J, mark: 5 name: A, mark: 7 name: H, mark: 8 |
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.