Introduction To Structures In C

Introduction to Structure in C

In simple words, a structure is a user-defined data type in C. With structures we can combine various data types to store a specific type of data. A structure helps us to store a complex data type in a more meaningful way. It is somewhat similar to an array. The only difference is that an array is used to store a collection of homogeneous data types whereas a structure can store a collection of heterogeneous types of data.

For example, we need to store the information about students like his/her name, Roll number and marks. We can store theat information separately. A structure gives us a way to store the collection of that information in one unit under one name since all that information is related to the student.

The keyword struct defines structures.

Syntax of Structures

  1. struct Structure_name  
  2. {  
  3.     data_type member1;  
  4.     data_type member2;  
  5.     data_type memeber;  
  6. }; 

We can create the structure for a person as said above as:

  1. struct student  
  2. {  
  3.     char Name[50];  
  4.     int Roll_no;  
  5.     int Marks;  
  6. }; 

When we define a structure, it only creates a user-defined type, but no memory is allocated. For memory allocation we need to declare variables of our structure type. For the preceding structure of student, a variable can be declared as:

  1. student s1, s2;  

If we need an array then we can declare an array of students as:

  1. student s[20];  

Or we can also use:

  1. struct student  
  2. {  
  3.     char Name[50];  
  4.     int Roll_no;  
  5.     int Marks;  
  6. }   
  7. s1, s2, s[20]; 

Accessing members of a structure

We can access the members of a structure in either of the following two ways.

  • Member/Period operator(.)
  • Structure pointer operator(->)

Any member of a structure can be accessed as: structure_name.memberName. Suppose we want to access marks of s2 then that would be s2.marks.

The pointer operator we will discuss later in this article.

Structures within Structures

Structures can be nested within other structures in C programming. Suppose we need to store a student address with student data. So we can modify the preceding structure as in the following:

  1. struct address  
  2. {  
  3.     char[50] locality;  
  4.     char[50] city;  
  5.     int pincode;  
  6. };  
  7. struct student  
  8. {  
  9.     char Name[50];  
  10.     int Roll_no;  
  11.     int Marks;  
  12.     struct address add;  
  13. } ;  
  14. student s1, s2; 

 

We can access locality for the s2 structure variable as:

  1. s1.add.locality ;

Passing Structure by value

A structure variable can be passed to the function as an argument as a normal variable. If a structure is passed by value then changes made in structure variable in the function is not reflect in the original structure variable in the calling function.

  1. void show(struct student st)  
  2. {  
  3.     printf("nStudent Name is :%s",st.Name);  
  4.     printf("n Roll No is: %d",st.Roll_no);  

Accessing Structure Members with Pointers

When we have a pointer of structure type, we must an arrow -> to access its members.

  1. int main()  
  2. {  
  3.     struct student st;  
  4.     struct student* ptr = &st;  
  5.     ptr->name = "Ratnesh"//Accessing Structure Members  
  6.     ptr-> Roll_no = 500;  


Similar Articles