Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Give the clarity on Pointer structures with suitable example in C language
Pointer to structure holds the address of an entire structure. Mainly, these are used to create complex data structures such as linked lists, trees, graphs and so on.
The members of the structure can be accessed by using a special operator called arrow operator ( -> ).
Syntax
struct tagname *ptr;
For example:
struct student *s;
Accessing Structure Members
You can access pointer to structure members using the arrow operator −
ptr->membername;
For example: s->sno, s->sname, s->marks
Example
The following C program demonstrates the use of pointer to structures −
#include <stdio.h>
struct student {
int sno;
char sname[30];
float marks;
};
int main() {
struct student s;
struct student *st;
printf("Enter sno, sname, marks: ");
scanf("%d %s %f", &s.sno, s.sname, &s.marks);
st = &s; // Assign address of structure to pointer
printf("\nDetails of the student are:
");
printf("Number = %d
", st->sno);
printf("Name = %s
", st->sname);
printf("Marks = %.2f
", st->marks);
return 0;
}
Output
Enter sno, sname, marks: 1 priya 34.5 Details of the student are: Number = 1 Name = priya Marks = 34.50
Key Points
- Pointer to structure is declared using
struct tagname *ptrsyntax. - The arrow operator (->) is used to access structure members through a pointer.
- Alternative syntax:
(*ptr).memberis equivalent toptr->member. - Pointer must be assigned the address of a structure before accessing members.
Conclusion
Pointers to structures are essential for creating dynamic data structures and efficient memory management. The arrow operator provides a convenient way to access structure members through pointers, making code more readable and maintainable.
