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;
}
Discussion:
60 comments Page 2 of 6.
Shubhankar said:
1 decade ago
It is the difference between deep copy and shallow copy.
@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'.
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?
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?
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.
Ravi Sharma said:
1 decade ago
Consider this program:
#include<stdio.h>
int main()
{
struct emp
{
char *n;
int age;
};
struct emp e1 = {"Dravid", 23};
struct emp e2 = e1;
e2.n="NEWSTRING";
printf("%s\n", e1.n);
return 0;
}
It shows output:
Dravid
Why not NEWSTRING ?
#include<stdio.h>
int main()
{
struct emp
{
char *n;
int age;
};
struct emp e1 = {"Dravid", 23};
struct emp e2 = e1;
e2.n="NEWSTRING";
printf("%s\n", e1.n);
return 0;
}
It shows output:
Dravid
Why not NEWSTRING ?
NoFear said:
1 decade ago
@Ravi Sharma and @Jyoti.
Consider the initialization of e1. e1 = {"Dravid", 23}.. Lets make it more clear by expanding (just for the understanding).
e1.n="Dravid";
e1.age=23
Now the "Dravid" string is stored in data section, and 'n' of e1 is pointing to that location address in data section.
When e2 is initialized to e1, i.e e2=e1, 'n' of e2 also points to the same address if data section.
Any changes to e2.n will affect the memory which is also pointed by e1.n. Hence the output is DRAVID.
Now coming to your doubt jyoti, e1.age is initialized with 23, age variable will be independent to both e1 and e2 hence two copies of age exists, any changes on e2.age wont be reflected back to e1.age.
Now Ravi Sharma, I guess you already got your answer by this time, if not then read on!!!
In your second assignment e2.n="NEWSTRING"; you are redirecting your pointer 'n' to point to a different memory location, but the value pointed by e1.n will be still intact and would be pointing to "Dravid" in data section.
Consider the initialization of e1. e1 = {"Dravid", 23}.. Lets make it more clear by expanding (just for the understanding).
e1.n="Dravid";
e1.age=23
Now the "Dravid" string is stored in data section, and 'n' of e1 is pointing to that location address in data section.
When e2 is initialized to e1, i.e e2=e1, 'n' of e2 also points to the same address if data section.
Any changes to e2.n will affect the memory which is also pointed by e1.n. Hence the output is DRAVID.
Now coming to your doubt jyoti, e1.age is initialized with 23, age variable will be independent to both e1 and e2 hence two copies of age exists, any changes on e2.age wont be reflected back to e1.age.
Now Ravi Sharma, I guess you already got your answer by this time, if not then read on!!!
In your second assignment e2.n="NEWSTRING"; you are redirecting your pointer 'n' to point to a different memory location, but the value pointed by e1.n will be still intact and would be pointing to "Dravid" in data section.
Keno said:
1 decade ago
I think it is the best way:
int main()
{
struct emp
{
char *n;
int age;
};
char var[7] = "Dravid";
struct emp e1 = { var, 23 };
struct emp e2 = e1;
_strupr_s(e2.n,20);
printf("%s\n", e1.n);
return 0;
}
And the output will be DRAVID, because e1.n and e2.n point the same literal (var), but e1 and e2 are not the same object.
The _strupr_s is equal with strupr
int main()
{
struct emp
{
char *n;
int age;
};
char var[7] = "Dravid";
struct emp e1 = { var, 23 };
struct emp e2 = e1;
_strupr_s(e2.n,20);
printf("%s\n", e1.n);
return 0;
}
And the output will be DRAVID, because e1.n and e2.n point the same literal (var), but e1 and e2 are not the same object.
The _strupr_s is equal with strupr
Ram said:
6 years ago
@Deepak.
Dot and arrow both access.
Dot and arrow both access.
Ratan said:
1 decade ago
The program has error since no <string.h> header file is included and string functions are being used.
Makraml ELGHAZZAOUI said:
1 decade ago
strupr cannot write on read-only address space "Dravid". Probably, program will exit with segmentation fault. If the member char*n was a table char n[30] for example then strupr can write it. But in this case, e1 and e2 will have different tables and the result of strupr on e1 will not affect e2.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers