Pointer and Structure
Similar to other type of Pointer, we can have a structure Pointer also. i.e. we can also point a pointer to a Structure. A pointer to a Structure variable can be declared by prefixing the variable name by ‘*’. Consider the following declaration,
struct abc
{
int a;
float b;
};
struct abc x; //Declare Structure variable x of type abc
struct abd *ptr; //Declare a Pointer ptr to a variable of type abc
It can be shown as below in fig.
Once this declaration is made, we can assign the address of the structure variable x to the structure Pointer ptr by the following statement
ptr = &x;
Since ptr is a Pointer to the structure variable x, the member of the structure can also ne accessed through a special operator called arrow operator i.e. ‘->’ (minus sign followed by greater than sign).
Programming Notes PDF
For Example, the members a and b of the structure variable x pointed by ptr can be assigned value 10 and 75.75 respectively by the following statement:
ptr -> a = 10;
ptr -> b = 75.75;
The arrow operator ‘->’ is also known as Pointer to Member operator.