C Programming - Pointers - Discussion
Discussion Forum : Pointers - General Questions (Q.No. 5)
5.
If a variable is a pointer to a structure, then which of the following operator is used to access data members of the structure through the pointer variable?
Discussion:
60 comments Page 1 of 6.
Mithra said:
2 months ago
If a variable is a pointer to a structure, the operator used to access data members of the structure through the pointer variable is: -> (Arrow operator)
Explanation:
When you have a pointer to a structure, like this:
struct Student
{
int id;
char name[20];
};
struct Student *ptr;
To access members of the structure using the pointer, you use:
ptr->id;
This is shorthand for:
(*ptr).id;
But -> is more convenient and cleaner.
For accessing members using normal structure variables.
-> For accessing members using structure pointers.
Explanation:
When you have a pointer to a structure, like this:
struct Student
{
int id;
char name[20];
};
struct Student *ptr;
To access members of the structure using the pointer, you use:
ptr->id;
This is shorthand for:
(*ptr).id;
But -> is more convenient and cleaner.
For accessing members using normal structure variables.
-> For accessing members using structure pointers.
Deepak said:
5 years ago
@All.
The eg Syntax is;
struct node
{
int data;
};
main(){
struct node p,*a;
P.data=10;
a=&p;
printf(" %d",a->data);
}
And Output is 10.
The eg Syntax is;
struct node
{
int data;
};
main(){
struct node p,*a;
P.data=10;
a=&p;
printf(" %d",a->data);
}
And Output is 10.
(5)
Swapnika said:
6 years ago
Dot (.) operator also used in pointers.
(3)
Priya said:
9 years ago
I did not understand this question. Please help me to get this.
(3)
Ashish said:
9 years ago
I think the answer given to the question is wrong.
We can use(*p).x instead of using ->.
We can use(*p).x instead of using ->.
(3)
Dhairya said:
9 years ago
Pointer is a collection of data;& operator access a memory in & by Code block.
Sumit said:
9 years ago
We can use either (*p).x or p->x where x is the member of the structure.
Sayli said:
9 years ago
Good explanation. Thanks @Kisan.
Jatin said:
1 decade ago
Here we can use arrow operator to access structure members without creating structure variable that's why we used -> (arrow) operator.
(1)
Nayan Shrimal said:
1 decade ago
Because it is a De-reference operator (->). So the data members can easily accessible.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers