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 4 of 6.
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.
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.
Sidhesh said:
1 decade ago
@Ramchandra
Can we write the above code with the help of "." operator ?
Can we write the above code with the help of "." operator ?
Chinna said:
2 decades ago
Because specify the next address.
Sangeetha said:
1 decade ago
Very good keep it up (master brain).
Apurva Nigam said:
1 decade ago
Thanks Kisan for properly explaining.
Kiran said:
1 decade ago
Well said kisan.
Vijaya said:
1 decade ago
When we create structure as a pointer, we have to access those structure members with structure object->structure variable.
Vinoth said:
1 decade ago
Clear explanation Kisan
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers