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;
}
pdt.month = 12
&pdt.month = 12
d.month = 12
pdt->month = 12
Answer: Option
Explanation:
No answer description is available. Let's discuss.
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

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

Tanya said:   1 decade ago
Hi Appu,
Here we are passing the address of d to pdt , and d is nothing but the object of the structure .pdt is pointer type so only -> opertaor is used to reference the member of the structure.

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.

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.

Kranti said:   1 decade ago
'.' is also used used access the elements of a structure. Hence I think, both A and D are correct.

Karthik said:   6 years ago
@All.

Here, It's told that assigning the value using 'pdt' variable.

So, option D is correct.

Sumit said:   1 decade ago
d.month=10;
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.

Appu said:   1 decade ago
If any one have any idea regarding this output of the program kindly give a comment.


Post your comments here:

Your comments will be displayed after verification.