Structure Pointer in C
A structure pointer is defined as the pointer which points to the address of the memory block that stores a structure known as the structure pointer. Complex data structures like Linked lists, trees, graphs, etc. are created with the help of structure pointers. The structure pointer tells the address of a structure in memory by pointing the variable to the structure variable.
Example:
C
// C program to demonstrate structure pointer #include <stdio.h> struct point { int value; }; int main() { struct point s; // Initialization of the structure pointer struct point* ptr = &s; return 0; } |
In the above code s is an instance of struct point and ptr is the struct pointer because it is storing the address of struct point.
Accessing the Structure Member with the Help of Pointers
There are two ways to access the members of the structure with the help of a structure pointer:
- With the help of (*) asterisk or indirection operator and (.) dot operator.
- With the help of ( -> ) Arrow operator.
Below is the program to access the structure members using the structure pointer with the help of the dot operator.
C
// C Program to demonstrate Structure pointer #include <stdio.h> #include <string.h> struct Student { int roll_no; char name[30]; char branch[40]; int batch; }; int main() { struct Student s1; struct Student* ptr = &s1; s1.roll_no = 27; strcpy (s1.name, "Kamlesh Joshi" ); strcpy (s1.branch, "Computer Science And Engineering" ); s1.batch = 2019; printf ( "Roll Number: %d\n" , (*ptr).roll_no); printf ( "Name: %s\n" , (*ptr).name); printf ( "Branch: %s\n" , (*ptr).branch); printf ( "Batch: %d" , (*ptr).batch); return 0; } |
1
Below is the program to access the structure members using the structure pointer with the help of the Arrow operator. In this program, we have created a Structure Student containing structure variable s. The Structure Student has roll_no, name, branch, and batch.
C
// C Program to demonstrate Structure pointer #include <stdio.h> #include <string.h> // Creating Structure Student struct Student { int roll_no; char name[30]; char branch[40]; int batch; }; // variable of structure with pointer defined struct Student s, *ptr; int main() { ptr = &s; // Taking inputs printf ( "Enter the Roll Number of Student\n" ); scanf ( "%d" , &ptr->roll_no); printf ( "Enter Name of Student\n" ); scanf ( "%s" , &ptr->name); printf ( "Enter Branch of Student\n" ); scanf ( "%s" , &ptr->branch); printf ( "Enter batch of Student\n" ); scanf ( "%d" , &ptr->batch); // Displaying details of the student printf ( "\nStudent details are: \n" ); printf ( "Roll No: %d\n" , ptr->roll_no); printf ( "Name: %s\n" , ptr->name); printf ( "Branch: %s\n" , ptr->branch); printf ( "Batch: %d\n" , ptr->batch); return 0; } |
Output:
Enter the Roll Number of Student 27 Enter Name of Student Kamlesh_Joshi Enter Branch of Student Computer_Science_And_Engineering Enter batch of Student 2019 Student details are: Roll No: 27 Name: Kamlesh_Joshi Branch: Computer_Science_And_Engineering Batch: 2019
Contact Us