C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Find Output of Program (Q.No. 8)
8.
What will be the output of the program in Turbo C (under DOS)?
#include<stdio.h>

int main()
{
    struct emp
    {
        char *n;
        int age;
    };
    struct emp e1 = {"Dravid", 23};
    struct emp e2 = e1;
    strupr(e2.n);
    printf("%s\n", e1.n);
    return 0;
}
Error: Invalid structure assignment
DRAVID
Dravid
No output
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
60 comments Page 1 of 6.

RISHI KUMAR said:   2 years ago
@All.

e1 and e2 both are objects and we are equating then e2=e1 --->means all the members of e1 we are equating with e1
e2.age=e1.age.
e2.n=e1.n.

Here the point is n is a char pointer variable that holds the base address so e1.n holds the base add of "Dravid" that same address we are storing in e2.n char are pointer.
So, e2.n and e1.n both are pointing to the same address.
(5)

Manitosh Paul said:   5 years ago
When a structure is assigned, passed, or returned, the copying is done monolithically. This means that the copies of any pointer fields will point to the same place as the original. In other words, anything pointed to is not copied.

Hence, on changing the name through e2.n it automatically changed e1.n.
(5)

Vrushali Vijay Bhoite said:   6 years ago
Thanks @Priyanka.

Ram said:   6 years ago
@Deepak.

Dot and arrow both access.

Deepak said:   7 years ago
I have one doubt here, using *n so in the printf statement we need write a1->n, but here writing a1.

Noel said:   7 years ago
No, strupr is an undeclared variable with or without string. Here; h causes the program to crash.
Remove strupr and you get the output Dravid - no CAPS just Dravid.

Varsha Kurpad said:   8 years ago
Answer will not b DRAVID. Because both e1 and e2 have different tables. How come the changes on e2 affects e1? Can anyone tel me please.

Mohd Saquib said:   8 years ago
As on compiling on GCC, no output. I got a Runtime error. Why? Please tell me.

Ramya said:   9 years ago
@Mohamed Mekawy.

Why there is no effect on 'toupper', why it simply prints 'Dravid' and not printing in uppercase?

Kmonahopoulos@hotmail.gr said:   9 years ago
1) You are missing the library #include<string.h>
2) The data in the executable char *n is read-only the way you define it so strupr won't work.
3) You must declare n as an array not as a char pointer in order for strupr to work.
4) You are changing variable e2.n and you are printing e1.n, so uppercase letters wont show up.

Execution :

#include<stdio.h>
#include<string.h>

int main()
{
struct emp
{
char n[7];
int age;
};
struct emp e1 = {"Dravid", 23};
struct emp e2 = e1;
strupr(e2.n);
printf("%s\n", e1.n);
return 0;
}
(4)


Post your comments here:

Your comments will be displayed after verification.