Structure type in C
A structured data type is a compound data type which falls under user-defined category and used for grouping simple data types or other compound data types. This contains a sequence of member variable names along with their type/attributes and they are enclosed within curl brackets.
Need for Struct data types
There are some situations when we need to group different types of variables in one group. Let's see one situation here- we want to store the name, roll and age of a student.
Example of Struct data types
Let's define these again with a structure type
Structure size and memory layout
Structure is user defined type to group of different type of variables of either compiler defined legacy types or other user defined types or mixed. Individual entity of a structure element is called member. Members inside a structure are placed sequentially next to next in the memory layout. Thus minimum size of a structure is the sum total of all sizes of members, with considering padding.
Struct data types syntax
typedef Struct in C
Struct datatypes are never used directly. A good programming practice is to typedef an meaningful alias. These typedef alias are used in source files. This enhances readability and understandability.
Nested structure
A nested structure is which contains one or more other structures inside it. Say we have a structure to hold the attributes of a student. Student will have attributes of a person or human being. Now we can define another structure called address. An address structure will have street, city, country and PIN code. Now each student should have an address so we can include a member called address of struct address_t in our student_t. Here student structure becomes a nested structure or composite structure.
Array of structure
Array of structure is a collection of same type of structure variables. Programmers can take an array of structures to view of modify elements. A loop of for or while or do-while may be needed to iterate each elements.
Another good advantage of using array of structure is saving or retrieving entire block of structures in files is very easy.
Pointer of structure
Pointer of structure is pointer type to point a structure variable. This can be used to point a point a global or local variable. Another use of taking a pointer of structure is to allocate dynamic array of structure.
Linked structure or linked list
Linked structure is one which contains address or pointer of the adjacent structure variable. Linked structure points to next structure and next structure does the same. So they form a chain of structures or often known as linked list.
Structure usage & applications
Structures are the most applicable field in any C program. Practical use of structure data type is count less. However below are some frequent applicable fields-
- Student, Employee, Customer etc data records,
- Objects displayed in Video games,
- Nodes in linked list, queue, LIFO,
- Binary tree and other Tree type objects
- File, Folders, volumes, partitions in file system and explorer nodes,
- All the GUI elements, Window, buttons, menu items,
- Header attributes of data files like, images (bitmaps, PNG, JPEG, GIF), sounds(wav, mp3, midi), video(mp4, avi, DIVX),
- Data structures used in OS (TASK, IPC, locks, device, driver etc),
- Frames, Packets,etc in TCP IP and OSI communication layers
- Any application or services handing objects.
Structure demo example
Program output
Enter the name, dept, roll number and marks of student: Student1 ECE 1 96.5 The name, dept, roll number and marks of the student are: Student1 ECE 1 96.50
About our authors: Team EQA
You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.