Thursday, 22 October 2015

C Arrow Operator

Arrow operator (->)

Arrow operator is used for accessing members of structure using pointer variable, below is the syntax of arrow operator in c programming –

Syntax of arrow operator

 struct student 
{
  char name[20],
  int roll; 
}*ptr;

Expalanation :

Whenever we declare structure variable then member can be accessed using the dot operator. But when pointer to a structure is used then arrow operator is used.

Both dot and arrow operator serves same function to access member of structure. Lets compare dot operator and arrow operator –

Example1:
 Struct student
{
    char name[20],
    int roll;
}std;
Example2: 
struct student
{
    char name[20]; 
    int roll;
}*ptr;


Access Structure MemberExample 1Example 2
Name is accessed usingstd.nameptr->name
Roll number is accessed usingstd.rollptr->roll

We can conclude that arrow operator is used to access the structure members when we use pointer variable to access it
In case if we want to access the members of structure using ordinary structure variable then we can use dot operator.

No comments:

Post a Comment