Subscribe Us

struct( ) Structures in C - definition -Initializing - examples


What is Structure in C programming language ?

Introducing to C structure

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 supports structure which allows you to wrap one or more variables with different data types. A structure can contain any valid data types like int, char, float even arrays or even other structures. Each variable in structure is called a structure member.

Defining structure

To define a structure, you use struct keyword. Here is the common syntax of structure definition: struct struct_name{ structure_member }; The name of structure follows the rule of variable name. Here is an example of defining address structure.

1 struct address {
2 unsigned int house_number;
3 char street_name[50];
4 int zip_code;
5 char country[50];
6 };

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

Declaring structure

The above example only defines an address structure without creating any structure instance. To create or declare a structure instance, you can do it in two ways:
The first way is to declare a structure followed by structure definition like this

1 struct struct_name {
2 structure_member;
3 ...
4 } instance_1,instance_2 instance_n;

In the second way, you can declare the structure instance at a different location in your source code after structure definition. Here is structure declaration syntax :

1 struct struct_name instance_1,instance_2 instance_n;

Complex structure

If a structure contains arrays or other structures, it is called complex structure. For example address structure is a structure. We can define a complex structure called customer which contains address structure as follows:

1 struct customer{
2 char name[50];
3 structure address billing_addr;
4 structure address shipping_addr;
5 };

Accessing structure member

To access structure members we can use dot operator (.) between structure name and structure member name as follows:
For example to access street name of structure address we do as follows:

1 struct address billing_addr;
2 = "US";

If the structure contains another structure, we can use dot operator to access nested structure and use dot operator again to access variables of nested structure.

1 struct customer jack;
2 = "US";

Initializing structure

C programming language treats a structure as a custom data type therefore you can initialize a structure like a variable. Here is an example of initialize product structure:

1 struct product{
2 char name[50];
3 double price;
4 } book = { "C programming language",40.5};

In above example, we define product structure, then we declare and initialize book structure with its name and price.

Structure and pointer

A structure can contain pointers as structure members and we can create a pointer to a structure as follows:

1 struct invoice{
2 char* code;
3 char date[20];
4 };
5 6 struct address billing_addr;
7 struct address *pa = &billing;_addr;

Shorthand structure with typedef keyword

To make your source code more concise, 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 when you want to create a n instance of it you can omit the keyword struct

1 typedef struct{
2 unsigned int house_number;
3 char street_name[50];
4 int zip_code;
5 char country[50];
6 } address;
7 8 address billing_addr;
9 address shipping_addr;

Copy a structure into another structure

One of major advantage of structure is you can copy it with = operator. The syntax as follows
1 struct_intance1 = struct_intance2
be noted that some old C compilers may not supports structure assignment so you have to assign each member variables one by one.

Structure and sizeof function

sizeof is used to get the size of any data types even with any structures. Let's take a look at simple program

01 #include
02 typedef struct __address{
03 int house_number;// 4 bytes
04 char street[50]; // 50 bytes
05 int zip_code; // 4 bytes
06 char country[20];// 20 bytes }
07 address;//78 bytes in total
08 void main() {
09 // it returns 80 bytes
10 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 function returns the size of structure larger than it is because the compiler pads struct members so that each one can be accessed faster without delays. So you should be careful when you read the whole structure from file which were written from other programs.

Source code example of using C structure

In this example, we will show you how to use structure to wrap student information and manipulate it by reading information to an array of student structure and print them on to console screen

01 #include
02 03 typedef struct _student{
04 char name[50];
05 unsigned int mark;
06 } student;
07 08 09 10 void print_list(student list[], int size);
11 void read_list(student list[], int size);
12 13 14 15 void main(){
16 17 const int size = 3;
18 student list[size];
19 20 read_list(list,size);
21 22 print_list(list,size);
23 24 25 }
26 27 void read_list(student list[], int size)
28 { 29 printf("Please enter the student information:\n");
30 31 for(int i = 0; i < size;i++){
32 printf("\nname:");
33 scanf("%S",&list;[i].name);
34 35 printf("\nmark:");
36 scanf("%U",&list;[i].mark);
37 }
39 }
40 41 void print_list(student list[], int size){
42 printf("Students' information:\n");
43 44 for(int i = 0; i < size;i++){
45 printf("\nname: %s, mark: %u",list[i].name,list[i].mark);
46 }
47 }


Please enter the student information:
Students' information:
name: J, mark: 5
name: A, mark: 7
name: H, mark: 8

Post a Comment