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.
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:
- Title
- Author
- Theme
- 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:
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:
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:
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?
You should read it
- What is the best URL structure for SEO?
- Property (Property) in C #
- What is Data Structure?
- Stack data structure (Stack)
- What is Silo Structure? Advantages and disadvantages of silos and effective alternatives
- C # program structure
- Queue data structure (Queue)
- New achievement: TSP chip structure can run 1 million billion operations per second
May be interested
- Set of multiple choice questions for programming with P15 prizeprogramming is a discipline that attracts many young people interested. not only that job opportunities after graduation with this job are extremely open. so, if you love programming, you should not ignore the following quiz series of network administrator.
- Set of multiple choice questions about programming with P10 prizethe programming questions below will give you lots of useful information. if you are interested in learning programming languages, the series of programming language topics will be very helpful for you.
- What is Data Structure?data structure is a way of storing, organized and systematic data organization so that data can be used effectively.
- Set of multiple choice questions about programming with P7 prizecurrent programming is no longer strange to us. programming work is becoming hot and more interested. please join the network administrator to learn about programming skills through multiple-choice questions below.
- Set of multiple-choice questions on award-winning programming P5serializing programming tests, in the following article, readers will be able to expand their knowledge with more interesting questions. let's start.
- Set of multiple choice questions about programming with P6the following network administrators will continue to send you interesting questions about programming. if you love this topic, then try your knowledge.
- P13 programming set of multiple choice questionsyou are a fan of programming languages and want to learn more about this topic. to give you an interesting reading about programming, in this article, the network administrator will send you a good quiz about this topic. invite your reference.
- Set of multiple choice questions on programming with P3 prizeinvite readers to participate in testing knowledge with questions around the topic of the following programming of network administrator. the question set will have 10 questions with 4 answers, please choose the most accurate answer.
- Set of multiple choice questions on programming with P16 prizemultiple choice questions about programming will be the first piece of knowledge to help you get started with programming. invite your reference.
- What is Silo Structure? Advantages and disadvantages of silos and effective alternativeseveryone knows that a properly structured website is good for both users and seo. silo structure is one of many ways that seo experts recommend people to apply for their website.