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 1 of 6.

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)

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)

Apurva Nigam said:   1 decade ago
@Raghushri:
We can access the elements of structures using "." also.
Eg:- obj.element

@ everybody:
Do we necessarily need to use "->" for a variable which is a pointer to a structure to access data members of the structure????
(2)

Maahi said:   1 decade ago
So finally which operator should be used? can any one tell?
(2)

Kisan said:   1 decade ago
. : used to access structure members with normal structure
variable.
-> : used to access structure members with pointer to structure

* : used to dereference a pointer.

& : used to obtain address of variable.
(2)

Manmeet said:   1 decade ago
When ever we want to fetch the value out of a pointer variable which is store in a structure.

We always use -> (arrow) pointer.

On the other hand if in a structure we have a simple variable of any data type we will use .(dot) operator to fetch the value from a variable.
(1)

Sufiyan said:   1 decade ago
Very good logic. Can you elaborate it?
(1)

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)


Post your comments here:

Your comments will be displayed after verification.