Pointer to Structure In C [Explained With Examples] – CsTutorialpoint
Hello friends, in today’s article we are going to talk about Pointer to Structure or Structure Pointer In C
Today we will learn in detail what is Pointer to Structure or Structure Pointer In C and what is its use in the C language.
So without wasting any time let’s understand Pointer to Structure In C
Pointer to Structure In C
As we know, the pointer is a variable that stores the address of another variable, which is of the same type, for example, if a pointer is of int type, then it only stores the address of a variable made of int data type. or and if a pointer is of double type then it will store the address of a variable of double type only.
Similarly, when a pointer stores the address of a structure variable, then it is called Pointer to structure or Structure Pointer.
Through Structure Pointer, we can point to such memory block which is storing the structure.
Declaration of Pointer to structure or Structure pointer
The structure pointer is declared in the same way as other structure variables are declared.
Syntax -:
struct structure_name *ptr;
Here ptr is a structure pointer which we can initialize separately while making declaration or even after declaration.
Example1
struct structure_name *ptr = &structure_variable;
Example2
ptr = &structure_variable;
Access Structure member using pointer
We can access the structure member in these ways -:
- Using indirection (*) operator and dot (.) operator.
- Using arrow (->) operator or membership operator.
Let’s now understand these two methods one by one.
Using indirection (*) operator and dot (.) operator
By indirection (*) operator and dot (.) operator, we access the structure in this way.
Example Program
#include<stdio.h>
#include<string.h>
struct student
{
char name[50];
int roll_no;
int age;
};
void main()
{
struct student s1;
struct student *ptr;
ptr = &s1;
//initialization of each member separately
strcpy(s1.name, "Jeetu") ; //copying string into char array
s1.roll_no = 73;
s1.age = 21;
printf( "student name %s, roll no %d and age = %d",(*ptr).name, (*ptr).roll_no, (*ptr).age);
}
In this program, Structure pointer ptr is accessing Structure member of Structure variable s1 by indirection (*) operator and dot (.) operator and printing their value.
Output -:
student name Jeetu roll no 73 and age = 21
Using arrow (->) operator or membership operator
By arrow (->) operator or membership operator, we access the structure in this way.
Example Program
#include<stdio.h>
#include<string.h>
struct student
{
char name[50];
int roll_no;
int age;
};
void main()
{
struct student s1;
struct student *ptr;
ptr = &s1;
//initialization of each member separately
strcpy(s1.name, "Jeetu") ; //copying string into char array
s1.roll_no = 73;
s1.age = 21;
printf( "student name %s, roll no %d and age = %d",ptr->name, ptr->roll_no, ptr->age);
}
In this program, Structure pointer ptr is accessing Structure member of Structure variable s1 by arrow (->) operator or membership operator and printing their value.
Read More -:
- Introduction of Pointer in C
- Address Operator (&) In C
- Indirection Operator(*) In C
- Pointer Arithmetic In C
- Pointer to Pointer in C
- Dangling Pointer in C
- Wild Pointer In C
- Null Pointer In C
- Void Pointer In C
- Dynamic Memory Allocation in C Programming
Conclusion
Friends, I hope you have found the answer to your question and you will not have to search about what is Pointer to Structure or Structure Pointer In C and what is its use in the C language.
However, if you want any information related to this post or related to programming language, or computer science, then comment below I will clear your all doubts
If you want a complete tutorial on C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about Pointer to Structure In C
To get more information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.