C - Structure - Struct
Structure and Linked List with C Programming in Tamil
Learn C - C tutorial - struct - C examples - C programs
C Structure - struct - Definition and Usage
- In C- Programming the Structure is defined as a collection of different data types which are grouped together and each element in a C structure is called member.
- In C-Programming Structure is similar to array but we can declare different data type in a single structure
Let's see the example to define structure for employee in c.
- struct is the keyword,
- employee is the tag name of structure,
- id, name and salary are the members or fields of the structure.
Declaring Structure Variable
- We can declare variable for the structure, so that we can access the member of structure easily.
- There are two ways to declare structure variable:
- By struct keyword within main() function
- By declaring variable at the time of defining structure.
Accessing Members of a Structure
- There are two ways to access structure members:
- By . (member or dot operator)
- By -> (structure pointer operator)
Sample - C Code
C Code - Explanation
- In this statement we create the structure with the structure name as “student” .
- Here in this statement we initialize the structure with its element value as zero.
- In this statement we assign the id value as “id=1” .
- In this statement we assign the name value as “xxxx” .
- In this statement we assign the percentage value as “86.5” .
- Here in this statement we print the value of the record id using structure name with its elements. In this program we define three printf statement which will print the record id, name & percentage value.
Sample Output - Programming Examples
- Here in this output the structure variable id, name and percentage values (1, XXXX and 86.500000) are printed using the printf statement as shown here in the console window.
- A structure can be nested inside another structure. In other words, the members of a structure can be of any other type including structure.
- There are two ways to define nested structure in c language:
- By separate structure
- By Embedded structure
- We can create 2 structures, but dependent structure should be used inside the main structure as a member.
- We can define structure within the structure also. It requires less code than previous way. But it can't be used in many structures.
- We can access the member of nested structure by
Array of Structures
- There can be array of structures in C programming to store many information of different data types.
- The array of structures is also known as collection of structures.