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 4 of 6.

Laxmi said:   1 decade ago
How it gives the output in capital letters?

Amit_Nawale said:   1 decade ago
char * n -this is pointer which stores address
int age - stores values.

so that when we assign
e2=e1

suppose address =1000 at which Dravid is stored

then

both
e1.n=1000 and

e2.=1000

and strupr(e2.n) here we pass reference not a value

hence changes at 1000 location takes place

both e1 and e2 points to 1000

hence changes will reflect in both the stucture.

Ranjith said:   1 decade ago
How can assign a string value to char variable?

Deepak choudhary said:   1 decade ago
The output will be Dravid not DRAVID.

as the value e2.n has been modified by the strupr() function, which has nothing to do with e1.n .
so the output is option 3. (Dravid)

Anmol said:   1 decade ago
main(){
int x,y;
x=10;
y=x;
y=20;
printf("%d\t%d",x,y);
}
output= 10 20

Pranav said:   1 decade ago
When we write e1.n then pointer and point to the string "dravid".

Now when we write e2=e1 it means e2 point the same object .if we make Change either with e2 or e1 the value of n and age will be affected.

e1.n="dravid" //n->"dravid" .
e2=e1; //e2 point same object as e1

strupr(e2.n); //n->"DRAVID"
e1.n now print which npoint to.

Naresh said:   1 decade ago
e2.n="hello";
printf("%s,,,%s",e1.n,e2.n);

The output is : Dravid,,,hello

Why it is not changing the e2.n if above o/p is correct ?

Hi_all said:   1 decade ago
Thanks Midhun.

Jain said:   1 decade ago
Hello friends,

Why gcc is not consider string.h ? and except it which header file is include to run it in gcc?

Priya said:   1 decade ago
Its showing segmentation fault!


Post your comments here:

Your comments will be displayed after verification.