Structure in C
Structure in c language is a user defined datatype that allows you to hold different type of elements.
Each element of a structure is called a member.
It works like a template in C++ and class in Java. You can have different type of elements in it.
It is widely used to store student information, employee information, product information, book information etc.
Defining structure
The struct keyword is used to define structure. Let's see the syntax to define structure in c.
Let's see the example to define structure for employee in c.
Here, struct is the keyword, employee is the tag name of structure; id, name and salary are the members or fields of the structure. Let's understand it by the diagram given below:
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.
1st way:
Let's see the example to declare structure variable by struct keyword. It should be declared within the main function.
Now write given code inside the main() function.
2nd way:
Let's see another way to declare variable at the time of defining structure.
Which approach is good
But if no. of variable are not fixed, use 1st approach. It provides you flexibility to declare the structure variable many times.
If no. of variables are fixed, use 2nd approach. It saves your code to declare variable in main() fuction.
Accessing members of structure
There are two ways to access structure members:
- By . (member or dot operator)
- By -> (structure pointer operator)
Let's see the code to access the id member of p1 variable by . (member) operator.
C Structure example
Let's see a simple example of structure in C language.
Output:
employee 1 id : 101 employee 1 name : Sonoo Jaiswal
Let's see another example of structure in C language to store many employees information.
Output:
employee 1 id : 101 employee 1 name : Sonoo Jaiswal employee 1 salary : 56000.000000 employee 2 id : 102 employee 2 name : James Bond employee 2 salary : 126000.000000
Nested Structure in C
Nested structure in c language can have another structure as a member. There are two ways to define nested structure in c language:
- By separate structure
- By Embedded structure
1) Separate structure
We can create 2 structures, but dependent structure should be used inside the main structure as a member. Let's see the code of nested structure.
As you can see, doj (date of joining) is the variable of type Date. Here doj is used as a member in Employee structure. In this way, we can use Date structure in many structures.
2) Embedded structure
We can define structure within the structure also. It requires less code than previous way. But it can't be used in many structures.
Accessing Nested Structure
We can access the member of nested structure by Outer_Structure.Nested_Structure.member as given below:
C Nested Structure example
Let's see a simple example of nested structure in C language.
Output:
employee id : 101 employee name : Sonoo Jaiswal employee date of joining (dd/mm/yyyy) : 10/11/2014
C Union
Like structure, Union in c language is a user defined datatype that is used to hold different type of elements.
But it doesn't occupy sum of all members size. It occupies the memory of largest member only. It shares memory of largest member.
Advantage of union over structure
It occupies less memory because it occupies the memory of largest member only.
Disadvantage of union over structure
It can store data in one member only.
Defining union
The union keyword is used to define union. Let's see the syntax to define union in c.
Let's see the example to define union for employee in c.
C Union example
Let's see a simple example of union in C language.
Output:
employee 1 id : 1869508435 employee 1 name : Sonoo Jaiswal
As you can see, id gets garbage value because name has large memory size. So only name will have actual value.
No comments:
Post a Comment