Structure (Struct) in C programming

Arrays in C allow you to define several types of variables that can hold the values ​​of several components of the same type. But the structure is another type of data in the C programming language, which allows you to combine other types of data.

Arrays in C allow you to define several types of variables that can hold the values ​​of several components of the same type. But the structure is another type of data in the C programming language, which allows you to combine other types of data.

The structure is used to represent a record. Suppose you want to store the value of a book in your library. You can store the properties of the following book:

  1. Title
  2. Author
  3. Theme
  4. ID (like your student ID)

Define a structure in C

To define structure, you must use struct statement. The struct statement defines a new data type, with more than one component in your program. The general form of the struct statement is as follows:

 struct [ ten_cau_truc ] { phan dinh nghia thanh vien cau truc ; phan dinh nghia thanh vien cau truc ; . phan dinh nghia thanh vien cau truc ; } [ mot hoac nhieu bien cau truc ]; 

A ten_cau_truc can be optional and a component is defined as common variables such as int i, float j or another variable definition . At the end of the structure definition, before the semicolon, you can correct define one or more structural variables (optional). Here's how to report the Book structure variable:

 struct Books { char tieude [ 50 ]; char tacgia [ 50 ]; char chude [ 100 ]; int id ; } book ; 

Access components of the structure in C

To access any component of the structure, you use the element access operator . Here is an example of how to use the structure:

 #include #include struct Books { char tieude [ 50 ]; char tacgia [ 50 ]; char chude [ 100 ]; int id ; }; int main ( ) { struct Books Book1 ; /* Khai bao Book1 la cua kieu Book */ struct Books Book2 ; /* Khai bao Book2 la cua kieu Book */ /* thong tin chi tiet quyen sach thu nhat */ strcpy ( Book1 . tieude , "Lap trinh C" ); strcpy ( Book1 . tacgia , "Pham Van At" ); strcpy ( Book1 . chude , "Ngon ngu lap trinh C" ); Book1 . id = 1234567 ; /* thong tin chi tiet quyen sach thu hai */ strcpy ( Book2 . tieude , "Toi thay hoa vang tren co xanh" ); strcpy ( Book2 . tacgia , "Nguyen Nhat Anh" ); strcpy ( Book2 . chude , "Van hoc" ); Book2 . id = 6677028 ; /* hien thi thong tin Book1 */ printf ( "Tieu de cua Book1 la: %sn" , Book1 . tieude ); printf ( "Tac gia cua Book1 la: %sn" , Book1 . tacgia ); printf ( "Chu de cua Book1 la: %sn" , Book1 . chude ); printf ( "ID cua Book1 la: %dn" , Book1 . id ); /* hien thi thong tin Book2 */ printf ( "Tieu de cua Book2 la: %sn" , Book2 . tieude ); printf ( "Tac gia cua Book2 la: %sn" , Book2 . tacgia ); printf ( "Chu de cua Book2 la: %sn" , Book2 . chude ); printf ( "ID cua Book2 la: %dn" , Book2 . id ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; } 

Compiling and running the above C program will result:

Structure (Struct) in C programming Picture 1Structure (Struct) in C programming Picture 1

Structures like function parameters

You can put the structure as a parameter of the function as easily as other variables or pointers. Access structure variables as shown below:

 #include #include struct Books { char tieude [ 50 ]; char tacgia [ 50 ]; char chude [ 100 ]; int id ; }; /* khai bao ham */ void inthongtinsach ( struct Books book ); int main ( ) { struct Books Book1 ; /* Khai bao Book1 la cua kieu Book */ struct Books Book2 ; /* Khai bao Book2 la cua kieu Book */ /* thong tin chi tiet quyen sach thu nhat */ strcpy ( Book1 . tieude , "Lap trinh C" ); strcpy ( Book1 . tacgia , "Pham Van At" ); strcpy ( Book1 . chude , "Ngon ngu lap trinh C" ); Book1 . id = 1234567 ; /* thong tin chi tiet quyen sach thu hai */ strcpy ( Book2 . tieude , "Toi thay hoa vang tren co xanh" ); strcpy ( Book2 . tacgia , "Nguyen Nhat Anh" ); strcpy ( Book2 . chude , "Van hoc" ); Book2 . id = 6677028 ; /* hien thi thong tin Book1 */ inthongtinsach ( Book1 ); /* hien thi thong tin Book2 */ inthongtinsach ( Book2 ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; } void inthongtinsach ( struct Books book ) { printf ( "Tieu de sach: %sn" , book . tieude ); printf ( "Tac gia: %sn" , book . tacgia ); printf ( "Chu de: %sn" , book . chude ); printf ( "Book ID: %dn" , book . id ); } 

Compiling and running the above C program will result:

Structure (Struct) in C programming Picture 2Structure (Struct) in C programming Picture 2

Cursor to structure

You can define pointer structure in the way you define other pointer types as follows:

 struct Books * struct_pointer ; 

You can now store the address of the structure variable in the pointer variable defined above. To find the address of a structure variable, set the operator & before the structure name as follows:

 struct_pointer = & Book1 ; 

To access the component of a structure using pointers to that structure, you must use the -> operator as follows:

 struct_pointer -> tieude ; 

Now we rewrite the example above using structured pointers, hoping this will be easy for you to understand this concept:

 #include #include struct Books { char tieude [ 50 ]; char tacgia [ 50 ]; char chude [ 100 ]; int id ; }; /* khai bao ham */ void inthongtinsach ( struct Books * book ); int main ( ) { struct Books Book1 ; /* Khai bao Book1 la cua kieu Book */ struct Books Book2 ; /* Khai bao Book2 la cua kieu Book */ /* thong tin chi tiet quyen sach thu nhat */ strcpy ( Book1 . tieude , "Lap trinh C" ); strcpy ( Book1 . tacgia , "Pham Van At" ); strcpy ( Book1 . chude , "Ngon ngu lap trinh C" ); Book1 . id = 1234567 ; /* thong tin chi tiet quyen sach thu hai */ strcpy ( Book2 . tieude , "Toi thay hoa vang tren co xanh" ); strcpy ( Book2 . tacgia , "Nguyen Nhat Anh" ); strcpy ( Book2 . chude , "Van hoc" ); Book2 . id = 6677028 ; /* in thong tin Book1 bang cach truyen dia chi cua Book1 */ inthongtinsach ( & Book1 ); /* in thong tin Book2 bang cach truyen dia chi cua Book2 */ inthongtinsach ( & Book2 ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; } void inthongtinsach ( struct Books * book ) { printf ( "Tieu de sach: %sn" , book -> tieude ); printf ( "Tac gia: %sn" , book -> tacgia ); printf ( "Chu de: %sn" , book -> chude ); printf ( "Book ID: %dn" , book -> id ); } 

Compiling and running the above C program will result:

Structure (Struct) in C programming Picture 3Structure (Struct) in C programming Picture 3

Bit fields (Bit Fields) in C

Bit fields allow packing data in a structure. It helps to optimize memory.

C allows you to do this in a structured definition by setting: the variable bit length . For example:

 struct packed_struct { unsigned int f1 : 1 ; unsigned int f2 : 1 ; unsigned int f3 : 1 ; unsigned int f4 : 1 ; unsigned int type : 4 ; unsigned int my_int : 9 ; } pack ; 

Here, packed_struct contains 6 elements: 4 elements f1.f4 is 1 bit, a type is 4 bits and a 9 bit my_int.

C automatically encapsulates the upper bit fields as neatly as possible, provided that the maximum length of this field is less than or equal to the computer's etymology length. You will learn about bit fields in a separate chapter.

According to Tutorialspoint

Last lesson: String in C

Next lesson: What is C programming language?

3.6 ★ | 9 Vote