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

Aswarth said:   1 decade ago
The Answer is Dravid. Because e2 is not a pointer. So not point to the e1. So any changes cannot be occur in e1.

Aakash said:   1 decade ago
Guys just a simple question.

e1 and e2 both are objects of same structure but they both occupy the different memory spaces. So when we are doing any change in e2, how it can affect e1?

@harish said:   1 decade ago
Structure is a value type. So in e2 there is a copy of e1 not a reference. So whatever changes made to e2 won't be reflected in e1. Hence answer is 'Dravid'.

Shubhankar said:   1 decade ago
It is the difference between deep copy and shallow copy.

Nikki said:   1 decade ago
I think for e2 a new memory will be created and takes the same value as that of e1. So when changes are made in e2 is not going to be reflected in e1. Then the answer should be "@dravid" right.

Kishore said:   1 decade ago
String functions are not supported in C. So its an error.

Undefined reference to 'main'.

Gopal Bansal said:   1 decade ago
In competitive exams our mind will not go towards e2=e1 so we will think that we are changing e2 string not e1 string so Be Aware from this question.

RiteshIIIT said:   1 decade ago
Thanks @Vinay for your explanation and I must say that this one is the proper explanation and I tried as you suggested and get the result. Here is my experimented code friends try to understand(for gcc compiler not sure abt turboC behaviour):

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

int main()
{
struct emp
{
char *n;
int age;
};

struct emp e1 = {"Dravid", 23};
struct emp e2 = e1;

static char *ptr,name[20];
int i=0;

ptr=e2.n;

printf("char pointer contains: %s\n",ptr);

while(*(ptr+i)!='\0'){
name[i]=*(ptr+i);
i++;
}

strupr(name);

printf("name array contains: %s\n",name);
printf("struct e1 contains: %s\n", e1.n);
printf("struct e2 contains: %s\n", e2.n);

return 0;
}

Output:
char pointer contains: Dravid
name array contains: DRAVID
struct e1 contains: Dravid
struct e2 contains: Dravid

Process returned 0 (0x0) execution time : 0.174 s

VijayNitj said:   1 decade ago
@Bhanu:

You are right that this program will give no output with SEGMENTATION ERROR, Because here in char*n , n is pointer to constant string so we can't change its value with strupr.

IN other case If we define char n[], it will print the value Dravid (gcc compiler) Not DRAVID bcoz in case of char n[], it only copies value of e1.n to e2.n.

PLEASE TRY TO UNDERSTAND THIS AND. IN PROGRAM IS WRONG AND AT LEAST TRY MY EXPLANATION.
(1)

Manoj said:   1 decade ago
@Bhanu is correct.

This code will not work.

n is a pointer (not a array) pointing to a constant string. If we declare n as "char n[20]", then only it will work, otherwise it gives segmentation fault.


Post your comments here:

Your comments will be displayed after verification.