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 6 of 6.
Ranjansharan said:
1 decade ago
Can anybody say what is the actual difference, when we acces the member of struct by using "." operator & "->" operator ?
Siddhant said:
1 decade ago
For structure...we have to point to the struct variables's data member..that why..-> is used....in structure for pointer representation.
Siva said:
1 decade ago
Thanks kisan.
Pichuka said:
1 decade ago
Pichuka says thanks to kissan.
PAVAN SHARATH said:
1 decade ago
There are two ways of accessing a structure member through a structure variable.
Ex:- struct student
{
int a;
float b;
}s1;
Here, S1 is a structure variable and a,b are the members.
**IF S1 IS A NORMAL STRUCTURE VARIABLE ,THEN IT CAN DIRECTLY ACCESS THE MEMBERS OF STRUCTURE!! THIS PROCESS IS CALLED DIRECT ACCESSING!!
FOR THIS PURPOSE WE USE '.'(DOT) OPERATOR.
EXAMPLE::- S1.A,S1.B ETC.
** BUT IF THE STRUCTURE VARIABLE S1 IS OF POINTER TYPE!!
i.e *S1,
THEN THIS STORES THE ADDRESS OF THE VARIABLE AND HENCE CANNOT BE DIRECTLY ACCESSED!! IT NEEDS TO BE ACCESSED INDIRECTLY THROUGH THE ADDRESS PRESENT WITH IT. THIS IS CALLED INDIRECT ACCESSING!!
FOR THIS PURPOSE WE USE '->'(ARROW) OPERATOR.
EXAMPLE ::- S1->A,S1->B etc.
FINALLY CONCLUSION:::-
IF STRUCTURE VARIABLE IS NORMAL, THEN WE USE '.'(DOT OPERATOR) ALSO CALLED AS DIRECT ACCESS OPERATOR.
SIMILARLY IF VARIABLE IS POINTER TYPE, THEN WE USE '->'(ARROW OPERATOR) ALSO CALLED AS INDIRECT ACCESS OPERATOR!!!
Ex:- struct student
{
int a;
float b;
}s1;
Here, S1 is a structure variable and a,b are the members.
**IF S1 IS A NORMAL STRUCTURE VARIABLE ,THEN IT CAN DIRECTLY ACCESS THE MEMBERS OF STRUCTURE!! THIS PROCESS IS CALLED DIRECT ACCESSING!!
FOR THIS PURPOSE WE USE '.'(DOT) OPERATOR.
EXAMPLE::- S1.A,S1.B ETC.
** BUT IF THE STRUCTURE VARIABLE S1 IS OF POINTER TYPE!!
i.e *S1,
THEN THIS STORES THE ADDRESS OF THE VARIABLE AND HENCE CANNOT BE DIRECTLY ACCESSED!! IT NEEDS TO BE ACCESSED INDIRECTLY THROUGH THE ADDRESS PRESENT WITH IT. THIS IS CALLED INDIRECT ACCESSING!!
FOR THIS PURPOSE WE USE '->'(ARROW) OPERATOR.
EXAMPLE ::- S1->A,S1->B etc.
FINALLY CONCLUSION:::-
IF STRUCTURE VARIABLE IS NORMAL, THEN WE USE '.'(DOT OPERATOR) ALSO CALLED AS DIRECT ACCESS OPERATOR.
SIMILARLY IF VARIABLE IS POINTER TYPE, THEN WE USE '->'(ARROW OPERATOR) ALSO CALLED AS INDIRECT ACCESS OPERATOR!!!
Bharath said:
1 decade ago
Thanks kisan
Pramod shinde said:
1 decade ago
Finaly to normal structure variable we can use "."operator and to pointer type variable we can use "->"Operator.
Kalidoss said:
2 decades ago
Pointers and Structures are used in the concept of Lined List, Stack, Queue ,etc.
In These Programs the address part or node is represented by the following
"obj->ptr"
In These Programs the address part or node is represented by the following
"obj->ptr"
Joshi said:
1 decade ago
To access data members of a structure we have to create a variable if thar variable if normal variable then we will use "."(dot) operator if thar is pointer variable then we will use "->"arrow operator.
example:
#include<stdio.h>
struct st
{
int x;
};
main()
{
struct st var1; creating normal variable
struct st *var2; creating pointer variable
//accessing data members of structure normal variable
scanf("%d",var1.x);
printf("%d",var1.x);
//accessing data membres of structure pointer vairables
scanf("%d",var1->x);
printf("%d",var1->x);
}
example:
#include<stdio.h>
struct st
{
int x;
};
main()
{
struct st var1; creating normal variable
struct st *var2; creating pointer variable
//accessing data members of structure normal variable
scanf("%d",var1.x);
printf("%d",var1.x);
//accessing data membres of structure pointer vairables
scanf("%d",var1->x);
printf("%d",var1->x);
}
Vishwanath sampath said:
1 decade ago
Pointer is a address of value. Then will find the address if you are submit that values.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers