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?
.
&
*
->
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
60 comments Page 6 of 6.

Nayan Shrimal said:   1 decade ago
Because it is a De-reference operator (->). So the data members can easily accessible.

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)

Sayli said:   9 years ago
Good explanation. Thanks @Kisan.

Sumit said:   9 years ago
We can use either (*p).x or p->x where x is the member of the structure.

Dhairya said:   9 years ago
Pointer is a collection of data;& operator access a memory in & by Code block.

Ashish said:   9 years ago
I think the answer given to the question is wrong.

We can use(*p).x instead of using ->.
(3)

Priya said:   9 years ago
I did not understand this question. Please help me to get this.
(3)

Swapnika said:   6 years ago
Dot (.) operator also used in pointers.
(3)

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.
(5)

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.


Post your comments here:

Your comments will be displayed after verification.