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.
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!!!
Ramachandran said:
1 decade ago
I hope that given simple program clear your doubts.
#include<stdio.h>
struct node { //creating structure with a variable(val)
int val;
};
typedef struct node item;
void main() {
item *t; // declaring a struct type pointer variable
t=(item *)malloc(sizeof(item)); //allocating memory to struct
type pointer variable(t)
t->val=20; //accessing structure member(val) by using (t)
printf("value is %d",t->val);
}
Here item *t; is struct type pointer variable.So,By using struct type pointer variable(t),you can access the members of the structure(val) using the symbol ->
eg:- t->val=20;
Just copy and execute the program.
#include<stdio.h>
struct node { //creating structure with a variable(val)
int val;
};
typedef struct node item;
void main() {
item *t; // declaring a struct type pointer variable
t=(item *)malloc(sizeof(item)); //allocating memory to struct
type pointer variable(t)
t->val=20; //accessing structure member(val) by using (t)
printf("value is %d",t->val);
}
Here item *t; is struct type pointer variable.So,By using struct type pointer variable(t),you can access the members of the structure(val) using the symbol ->
eg:- t->val=20;
Just copy and execute the program.
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);
}
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.
RaghuShri said:
1 decade ago
"." is used to access elements of a class via an object of class,eg-
class cls
{
public int a;
//some other work in this class
}
void main()
{
cls cls1=new cls();
cls1.a=20;//using "." operator here
}
and -> is used to access the elements of structures( it includes linked lists queues and stacks) eg- obj->element, you can guess the example at least rihht? take care, bye bye
class cls
{
public int a;
//some other work in this class
}
void main()
{
cls cls1=new cls();
cls1.a=20;//using "." operator here
}
and -> is used to access the elements of structures( it includes linked lists queues and stacks) eg- obj->element, you can guess the example at least rihht? take care, bye bye
(1)
Puja said:
1 decade ago
->(spangles) = accesses the pointer variable that contains the address of another variable.
*(as-trick) = points the value of the variable declared.
&(ampersand) = points the address of the variable declared
enum(enumeration) = user-defined data type.
eg:
{
int a[10]={1,4,5);
printf("%d",a[3]);
}
output is 6.
Explanation: successive no.of 5 is 6.
*(as-trick) = points the value of the variable declared.
&(ampersand) = points the address of the variable declared
enum(enumeration) = user-defined data type.
eg:
{
int a[10]={1,4,5);
printf("%d",a[3]);
}
output is 6.
Explanation: successive no.of 5 is 6.
JYOTI NAGPAL said:
1 decade ago
Yes, Its ans is D ex:
struct student
{
char name[20];
int rn;
}*s1;
If we access these data members in main then use,
//INPUT FROM KEYBOARD.
scanf("%s%d",s1->name,&s1->rn);
//DISPLAY THE OUTPUT.
printf("\nname= %s\troll no = %d",s1->name,s1->rn);
struct student
{
char name[20];
int rn;
}*s1;
If we access these data members in main then use,
//INPUT FROM KEYBOARD.
scanf("%s%d",s1->name,&s1->rn);
//DISPLAY THE OUTPUT.
printf("\nname= %s\troll no = %d",s1->name,s1->rn);
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.
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)
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????
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)
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.
variable.
-> : used to access structure members with pointer to structure
* : used to dereference a pointer.
& : used to obtain address of variable.
(2)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers