Structures in C Programming
Last Updated Nov 20, 2015, 07:00:14 PM
Strucutres are the most convenient way to store related data, for example, if you want to store the data of a classroom you might need to store the data of students, their details, roll numbers, marks etc...
So when you store such kind of related data, accessing it will become more difficult. But using the structures, we can reduce that problem at very scalable way. As we already, structures makes this job easier by stored different data under one group or one structure.
Structures must be declared under one common name which can be further used to call or use that data by calling the structure name anywhere in the program
Let us see how we can declare a strucure in c programming
If you notice the above syntax, we have a structure name, and data variables. Let us see how it would look like if we apply the structure for a student record in class
In the above example,
student_name, course_name, marks, age, student_grade are considered as member data of that strucutre.
The sttucture tag is student
Example of a StructureTry It Now
During the declaration of a structure, it creates some user-defined data, however, if we need more data variables to store data we can define them as below.
In the above example code,
s1,s2, and s3 are new variables we defined with no data which can store
an array of 30 elements
How to Access Structure Data
The structure data can be accessed in two different ways, let us see them
- Member Operator ( . )
- Strucute Pointer Operator ( -> )
The below syntax explain how to access any type of data variables using the structure.
Let us say, we want to access the age of a student s1, we can simply call like this