Home » Structure Pointer in C

Structure Pointer in C

by Online Tutorials Library

Structure Pointer in C

In this section, we will discuss the Structure pointer in the C programming language. Before going to the concepts, let’s understand the Structure. The structure is the collection of different data types grouped under the same name using the struct keyword. It is also known as the user-defined data type that enables the programmer to store different data type records in the Structure. Furthermore, the collection of data elements inside the Structure is termed as the member.

For example, suppose we want to create the records of a person containing name, age, id, city, etc., and these records cannot be grouped in the single dimension array. Therefore, we use the Structure to store multiple collections of data items.

Structure Pointer in C

Syntax to define Structure

Here structure_name is the name of the Structure that is defined using the struct keyword. Inside the structure_name, it collects different data types (int, char, float) elements known as the member. And the last, str, is a variable of the Structure.

Program to demonstrate the structure and access their member

In this program creates a student structure and access its member using structure variable and dot (.) operator.

struct.c

Output:

Name of the student s1 is: John  Roll No. of the student s1 is: 1101   state of the student s1 is: Los Angeles  Age of the student s1 is: 20  Name of the student s1 is:  Mark Douglas  Roll No. of the student s1 is: 111   The state of the student s1 is: California  Age of the student s1 is: 18  

Explanation of the program: As we can see in the above program, we have created a structure with name student, and the student structure contains different members such as name (char), roll_no (int), state (char), age (int). The student structure also defines two variables like s1 and s2, that access the structure members using dot operator inside the main() function.

Structure Pointer

The structure pointer points to the address of a memory block where the Structure is being stored. Like a pointer that tells the address of another variable of any data type (int, char, float) in memory. And here, we use a structure pointer which tells the address of a structure in memory by pointing pointer variable ptr to the structure variable.

Declare a Structure Pointer

The declaration of a structure pointer is similar to the declaration of the structure variable. So, we can declare the structure pointer and variable inside and outside of the main() function. To declare a pointer variable in C, we use the asterisk (*) symbol before the variable’s name.

After defining the structure pointer, we need to initialize it, as the code is shown:

Initialization of the Structure Pointer

We can also initialize a Structure Pointer directly during the declaration of a pointer.

As we can see, a pointer ptr is pointing to the address structure_variable of the Structure.

Access Structure member using pointer:

There are two ways to access the member of the structure using Structure pointer:

  1. Using ( * ) asterisk or indirection operator and dot ( . ) operator.
  2. Using arrow ( -> ) operator or membership operator.

Program to access the structure member using structure pointer and the dot operator

Let’s consider an example to create a Subject structure and access its members using a structure pointer that points to the address of the Subject variable in C.

Pointer.c

Output:

Subject Name:  Computer Science   Subject Id: 1201   Duration of the Subject: 6 Months   Type of the Subject:  Multiple Choice Question  

In the above program, we have created the Subject structure that contains different data elements like sub_name (char), sub_id (int), sub_duration (char), and sub_type (char). In this, the sub is the structure variable, and ptr is the structure pointer variable that points to the address of the sub variable like ptr = &sub. In this way, each *ptr is accessing the address of the Subject structure’s member.

Program to access the structure member using structure pointer and arrow (->) operator

Let’s consider a program to access the structure members using the pointer and arrow (->) operator in C.

Pointer2.c

Output:

Enter the name of the Employee (emp1): John   Enter the id of the Employee (emp1): 1099   Enter the age of the Employee (emp1): 28   Enter the gender of the Employee (emp1): Male   Enter the city of the Employee (emp1): California     Second Employee:     Enter the name of the Employee (emp2): Maria   Enter the id of the Employee (emp2): 1109   Enter the age of the Employee (emp2): 23   Enter the gender of the Employee (emp2): Female   Enter the city of the Employee (emp2): Los Angeles     Display the Details of the Employee using Structure Pointer   Details of the Employee (emp1)    Name: John   Id: 1099   Age: 28   Gender: Male   City: California     Details of the Employee (emp2) Name: Maria   Id: 1109   Age: 23   Gender: Female   City: Los Angeles  

In the above program, we have created an Employee structure containing two structure variables emp1 and emp2, with the pointer variables *ptr1 and *ptr2. The structure Employee is having the name, id, age, gender, and city as the member. All the Employee structure members take their respective values from the user one by one using the pointer variable and arrow operator that determine their space in memory.


Next Topicsprintf() in C

You may also like