STRUCTURE POINTER
A pointer of basic or user-defined data type can be member of a structure and even a pointer to a structure can be created.
//Example program to show how a pointer can be a member of a structure
#include
struct data
{
int x; //Stores address of integer Variable
int *ptr;
};
int main()
{
struct data d1;
d1.ptr=&d1.x;
*d1.ptr=10;
printf(“Value of x inside d1 is %d”,*d1.ptr);
return(0);
}
When a pointer to structure type is created it can store address of any variable of the same structure. Now the question is how to access the members of structure using pointer. For this, there are two solutions.
- Using * and . operators together
- Using arrow(->) or membership operator
//Example program to show how a pointer to a structure can be created
#include
struct data
{
int x,y; //Stores address of integer Variable
};
int main()
{
struct data *sptr,d1;
sptr=&d1;
(*sptr).x=10;
sptr->y=20;
printf(“Value of x inside d1 is %d”,sptr->x);
printf(“\nValue of y inside d1 is %d”,(*sptr).y);
return(0);
}