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 2 of 6.
RaghuShri said:
2 decades 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)
Sarvan said:
2 decades ago
@indhu
Pointer is just the address value, where the variable is stored. To get the address value we use "->" its just an representation. Hope you understand.
Pointer is just the address value, where the variable is stored. To get the address value we use "->" its just an representation. Hope you understand.
(1)
Indhu said:
2 decades ago
I didn't understand.
Can you explain it clearly.
Can you explain it clearly.
(1)
Mithra said:
12 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.
(1)
Dhanalakshmi said:
1 decade ago
An individual structure member can be accessed in terms of its corresponding pointer variable by writing.
ptr -> member.
ptr -> member.
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);
Bhargavi said:
1 decade ago
enum is a one of the userdefined datatype,and it defines the list of keywords.
Ex:
enum rainbow
{
1.Red
2.Orange
3.Yellow
4.Green
5.Blue
6.Indigo
7.Violet
} ;
Ex:
enum rainbow
{
1.Red
2.Orange
3.Yellow
4.Green
5.Blue
6.Indigo
7.Violet
} ;
Chandru said:
1 decade ago
Can we make use of ". " operator instead of "->" operator?
Haarika said:
1 decade ago
What is enum? in what situation we use it? can anybody help me out? please.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers