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 5 of 6.
Wikiok said:
1 decade ago
(*myPointer).element <=equals=> myPointer->element
Rekha said:
1 decade ago
Easily understandable good.
Hrudaya said:
1 decade ago
Thank u everyone for the explanation
Sachincs033 said:
1 decade ago
Thanks buddies.
AukI said:
1 decade ago
Kan you give some example @Kisan, you provided information is very understandable but it would be lot better, if you can post some examples to your thoughts.
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.
Nitin verma said:
1 decade ago
-> indicates direction toward's something and pointer is used for that in c.
Supriya said:
1 decade ago
Can any one help me by telling how execute programs on India bix (c) through online compiler by giving my own input at command prompt.
Rakesh said:
1 decade ago
@sidesh:
In the above code instead of writing t->val=20;,
we can write
(*t).value=20; thats it..
In the above code instead of writing t->val=20;,
we can write
(*t).value=20; thats it..
Sanket joshi said:
1 decade ago
. operator use when we have to acces stru member with array.
-> when we have to acces with pointer.
-> when we have to acces with pointer.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers