C Programming - Structures, Unions, Enums - Discussion
Discussion Forum : Structures, Unions, Enums - Point Out Correct Statements (Q.No. 3)
3.
Which of the following statements correctly assigns 12 to month using pointer variable pdt?
#include<stdio.h>
struct date
{
int day;
int month;
int year;
};
int main()
{
struct date d;
struct date *pdt;
pdt = &d;
return 0;
}
Discussion:
14 comments Page 1 of 2.
CrownedEagle said:
6 years ago
Both d.month and pdt->month will work fine.
Since d is a object of type struct date, we use '.' in d.month to access or modify value of month of object d.
whereas since pdt is pointer pointing to 'object d' we use pdt->month to access/modify the same.
Code below will print the same output regardless what operator you use.
Code:-
#include<stdio.h>
struct date
{
int day;
int month;
int year;
};
int main()
{
struct date d;
struct date *pdt;
pdt = &d;
pdt->month = 9;
printf("d.month = %d\n", d.month);
printf("pdt->month = %d\n", pdt->month);
d.month = 1;
printf("d.month = %d\n", d.month);
printf("pdt->month = %d\n", pdt->month);
return 0;
}
OUTPUT:-
d.month = 9
pdt->month = 9
d.month = 1
pdt->month = 1
Since d is a object of type struct date, we use '.' in d.month to access or modify value of month of object d.
whereas since pdt is pointer pointing to 'object d' we use pdt->month to access/modify the same.
Code below will print the same output regardless what operator you use.
Code:-
#include<stdio.h>
struct date
{
int day;
int month;
int year;
};
int main()
{
struct date d;
struct date *pdt;
pdt = &d;
pdt->month = 9;
printf("d.month = %d\n", d.month);
printf("pdt->month = %d\n", pdt->month);
d.month = 1;
printf("d.month = %d\n", d.month);
printf("pdt->month = %d\n", pdt->month);
return 0;
}
OUTPUT:-
d.month = 9
pdt->month = 9
d.month = 1
pdt->month = 1
CrownedEagle said:
6 years ago
Both d.month and pdt->month will work fine.
Since d is a object of type struct date, we use '.' in d.month to access or modify value of month of object d.
whereas since pdt is pointer pointing to 'object d' we use pdt->month to access/modify the same.
Code below will print the same output regardless what operator you use.
Code:-
#include<stdio.h>
struct date
{
int day;
int month;
int year;
};
int main()
{
struct date d;
struct date *pdt;
pdt = &d;
pdt->month = 9;
printf("d.month = %d\n", d.month);
printf("pdt->month = %d\n", pdt->month);
d.month = 1;
printf("d.month = %d\n", d.month);
printf("pdt->month = %d\n", pdt->month);
return 0;
OUTPUT:-
d.month = 9
pdt->month = 9
d.month = 1
pdt->month = 1
Since d is a object of type struct date, we use '.' in d.month to access or modify value of month of object d.
whereas since pdt is pointer pointing to 'object d' we use pdt->month to access/modify the same.
Code below will print the same output regardless what operator you use.
Code:-
#include<stdio.h>
struct date
{
int day;
int month;
int year;
};
int main()
{
struct date d;
struct date *pdt;
pdt = &d;
pdt->month = 9;
printf("d.month = %d\n", d.month);
printf("pdt->month = %d\n", pdt->month);
d.month = 1;
printf("d.month = %d\n", d.month);
printf("pdt->month = %d\n", pdt->month);
return 0;
OUTPUT:-
d.month = 9
pdt->month = 9
d.month = 1
pdt->month = 1
Karthik said:
6 years ago
@All.
Here, It's told that assigning the value using 'pdt' variable.
So, option D is correct.
Here, It's told that assigning the value using 'pdt' variable.
So, option D is correct.
Taran rishit said:
7 years ago
(*d).month is valid I think.
Reetika said:
8 years ago
When we are using the pointer for pointing out the structure then we should use the arrow indicator instead of (.) operator.
Pravin pandhare said:
9 years ago
d.month=10;
printf("%d",d.month);
Output: 10.
printf("%d",d.month);
Output: 10.
Kranti said:
1 decade ago
'.' is also used used access the elements of a structure. Hence I think, both A and D are correct.
Abhayraj said:
1 decade ago
-> (direction operator or member selection via pointer operator).
Is used to access the members of the structure by its pointer variable (object of structure).
Plz, correct me for any mistake.
Is used to access the members of the structure by its pointer variable (object of structure).
Plz, correct me for any mistake.
Sumit said:
1 decade ago
d.month=10;
printf("%d",d.month);
Output: 10
It is also correct. So, why only D ?
printf("%d",d.month);
Output: 10
It is also correct. So, why only D ?
Gopal said:
1 decade ago
Here pdt pointer variable which is hold address of d so -> are used to access value.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers