Summary: in this tutorial, you will learn how to define a new type called C structure that allows you to wrap related variables with different types into a single entity.
Introduction to C structure
When you design software, 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 applications, you need a new form of variable that reflects the real-world data.
For example, you may want to reference an address that holds multiple fields including house number, street, zip code, state, and country.
To do it, C provides you with the structure type. A structure allows you to wrap related variables with different data types into a single variable.
Defining structure
To define a structure, you use the struct
keyword. The following shows the syntax for defining a structure:
struct structure_name
{
field_type field_name;
field_type field_name;
field_type field_name;
// ...
};
Code language: C++ (cpp)
In this syntax:
- First, use the struct keyword followed by the structure name.
- Second, specify the elements of the structure including type and name. The elements of the structure are also called fields.
For example, the following defines the address
structure:
struct address
{
unsigned int house_number;
char street_name[120];
char city[50];
char state[2];
int zip_code;
char country[50];
};
Code language: C++ (cpp)
The address
structure includes house number, street name, city, state, zip code, and country.
Declaring structure variables
The above example defines the address
structure without declaring any structure variables. C provides you with two ways to declare structure variables:
1) Declaring structure variables with the structure definition
The follownig shows the syntax for declaring structure variables with the structure definition:
struct structure_name
{
field_type field_name;
field_type field_name;
field_type field_name;
// ...
} struct_var1, struct_var2;
Code language: C++ (cpp)
In this syntax, you can omit the structure name like this:
struct
{
field_type field_name;
field_type field_name;
field_type field_name;
// ...
} struct_var1, struct_var2;
Code language: C++ (cpp)
For example, you can define the address
structure and declare two structure variables:
struct address
{
unsigned int house_number;
char street[120];
char city[50];
char state[3];
int zip_code;
char country[50];
} home_address, business_address;
Code language: C++ (cpp)
In this example, the home_address
and business_address
are the structure variables. Alternatively, you can declare the structure variables as follows:
struct
{
unsigned int house_number;
char street[120];
char city[50];
char state[3];
int zip_code;
char country[50];
} home_address, business_address;
Code language: C++ (cpp)
2) Declaring structure variables after defining the structure
The following shows the syntax for defining a structure and its variables:
struct structure_name
{
field_type field_name;
field_type field_name;
field_type field_name;
// ...
};
struct structure_name struct_var1, struct_var2;
Code language: C++ (cpp)
In this syntax, you separate the structure definition from the variable declaration. For example:
struct address
{
unsigned int house_number;
char street[120];
char city[50];
char state[3];
int zip_code;
char country[50];
};
struct address home_address, business_address;
Code language: C++ (cpp)
Initializing structure variables
Like a regular variable, you can initialize a structure variable. The following example shows the syntax for initializing a structure variable:
struct structure_name
{
field_type field_name;
field_type field_name;
field_type field_name;
// ...
} struct_var = {
value,
value,
value,
/// ...
};
Code language: C++ (cpp)
For example, you can define a structure, declare a structure variable, and initialize the structure variable like this:
struct address
{
unsigned int house_number;
char street[120];
char city[50];
char state[2];
int zip_code;
char country[50];
} business_address = {
3960,
"North 1st Street",
"San Jose",
"CA",
95134,
"USA"
};
Code language: C++ (cpp)
Accessing structure members
To access a structure member, you use the dot operator (.
) as follows:
Code language: C++ (cpp)structure_name.field_name
The following programm print out the fields of the address structure:
#include <stdio.h>
int main()
{
struct address
{
unsigned int house_number;
char street[120];
char city[50];
char state[3];
int zip_code;
char country[50];
} business_address = {
3960,
"North 1st Street",
"San Jose",
"CA",
95134,
"USA"};
// print out the address
printf("%d, %s\n", business_address.house_number, business_address.street);
printf("%s\n", business_address.city);
printf("%s, %d\n", business_address.state, business_address.zip_code);
printf("%s\n", business_address.country);
}
Code language: C++ (cpp)
Output:
3960, North 1st Street
San Jose
CA, 95134
USA
Code language: C++ (cpp)
Structures that contain other structures
As mentioned earlier, a structure may contain another structure. For example, you can define the customer
structure that contains the address
structures:
struct customer
{
char name[100];
struct address shipping_address;
struct address billing_address;
};
Code language: C++ (cpp)
In this example, the customer structure contains two members, shipping_address
and billing_address
which are also the structures.
When a structure contains a structure, you use the dot operator ( .
) to access the fields of the nested structure.
The following example defines the customer structure and variable (john
). It then copies the string "San Fransisco"
to the city of the shipping address of the customer:
struct customer
{
char name[100];
struct address shipping_address;
struct address billing_address;
} john;
strcpy(john.shipping_address.city, "San Fransisco");
Code language: C++ (cpp)
Assigning a structure into another structure
C allows you to assign a structure to another by using the assignment operator (=
):
Code language: C++ (cpp)structure_variable1 = structure_variable2;
Notice that some old C compilers may not support structure assignment. If this is the case, you have to assign each structure member individually.
The following program illustrates how to assign the business_address
structure to the home_address
structure using the assignment operator (=
):
#include <stdio.h>
#include <string.h>
int main()
{
struct address
{
unsigned int house_number;
char street[120];
char city[50];
char state[3];
int zip_code;
char country[50];
} home_address;
struct address business_address = {
3960,
"North 1st Street",
"San Jose",
"CA",
95134,
"USA"};
// assign business address to home address
home_address = business_address;
// change the house number
home_address.house_number = 4000;
// show the home address
printf("%d, %s\n", home_address.house_number, home_address.street);
printf("%s\n", home_address.city);
printf("%s, %d\n", home_address.state, home_address.zip_code);
printf("%s\n", home_address.country);
}
Code language: C++ (cpp)
Output:
4000, North 1st Street
San Jose
CA, 95134
USA
Code language: C++ (cpp)
C structure type and the sizeof operator
To get the size of a structure, you cannot just simply add up the size of its fields. Instead, you use the sizeof()
operator like the following example:
#include <stdio.h>
int main()
{
struct address
{
unsigned int house_number;
char street[120];
char city[50];
char state[3];
int zip_code;
char country[50];
} business_address;
// show the size of the address structure
printf("The size of address structure is %d bytes\n", sizeof(business_address));
// calculate the total size of a structure members
size_t size = 0;
size += sizeof(business_address.house_number);
size += sizeof(business_address.street);
size += sizeof(business_address.city);
size += sizeof(business_address.state);
size += sizeof(business_address.zip_code);
size += sizeof(business_address.country);
printf("The total size of the address members is %d bytes\n", size);
}
Code language: C++ (cpp)
Output:
The size of address structure is 236 bytes
The total size of the address members is 231 bytes
Code language: C++ (cpp)
The sizeof()
operator returns the size of a structure that is always bigger than the size of all the structure members.
The reason is that the compilers always need to pad the structure members so that each member can be accessed faster.
Structure shorthand with typedef keyword
The typedef keyword allows you to assign a new type name to an existing type. For example, you can assign a structure a new type name and use that type to declare a variable.
The following example uses the typedef
keyword to define the address
structure type:
typedef struct
{
unsigned int house_number;
char street[120];
char city[50];
char state[3];
int zip_code;
char country[50];
} address;
Code language: C++ (cpp)
The address is a new type now. So you can use it to declare variables like this:
Code language: C++ (cpp)address business_address;
The typedef
makes your code less verbose and more concise.
C structure example
The following program illustrates how to use the structure to read the product information including name and quantity from the command line and display the total quantity of all the products:
#include <stdio.h>
typedef struct
{
char name[50];
unsigned int quantity;
} product;
void read_product(product product_list[], const unsigned int size);
unsigned int get_total_quantity(product product_list[], const unsigned int size);
int main()
{
const unsigned int SIZE = 3;
int total_quantity = 0;
product product_list[SIZE];
read_product(product_list, SIZE);
total_quantity = get_total_quantity(product_list, SIZE);
printf("The total quantity is %u", total_quantity);
}
// read the product information from the command line
void read_product(product product_list[], const unsigned int size)
{
for (unsigned int i = 0; i < size; i++)
{
printf("Product name:");
scanf("%s", &product_list[i].name);
printf("Quantity:");
scanf("%u", &product_list[i].quantity);
}
}
// get the total quantity of all products
unsigned int get_total_quantity(product product_list[], const unsigned int size)
{
// calculate the total quantity
unsigned int total_quantity = 0;
for (int i = 0; i < size; i++)
total_quantity += product_list[i].quantity;
return total_quantity;
}
Code language: C++ (cpp)
Output:
Product name:A
Quantity:1
Product name:B
Quantity:2
Product name:C
Quantity:3
The total quantity is 6
Code language: C++ (cpp)
To change the number of products that you want to enter, you need to change the SIZE
constant.
Summary
- A structure type combines multiple related fields with different types.
- Use the
struct
keyword to define a structure. - Use the dot operator (.) to access the members of the structure.