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.
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 ?
Mohamed mekawy said:
1 decade ago
#include<stdio.h>
int main()
{
struct emp
{
char *n;
int age;
};
struct emp e1 = {"Dravid", 23};
struct emp e2 = e1;
toupper(e2.n);
printf("%s\n", e1.n);
return 0;
}
The output is "Dravid".
int main()
{
struct emp
{
char *n;
int age;
};
struct emp e1 = {"Dravid", 23};
struct emp e2 = e1;
toupper(e2.n);
printf("%s\n", e1.n);
return 0;
}
The output is "Dravid".
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.
Hence, on changing the name through e2.n it automatically changed e1.n.
(5)
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.
Jyoti said:
1 decade ago
Hello,It don't have any connection with string library.
As 2 parameters is passing,So 2nd parameter i.e. 23 which was earlier initialized get displayed.
And e2.age+=3;
means e2.age=e2.age+3;
and
struct emp e2=e1;
means e1 is assigned to e2 So 23+3=26
Hope I have explained clearly.
As 2 parameters is passing,So 2nd parameter i.e. 23 which was earlier initialized get displayed.
And e2.age+=3;
means e2.age=e2.age+3;
and
struct emp e2=e1;
means e1 is assigned to e2 So 23+3=26
Hope I have explained clearly.
Bp123 said:
1 decade ago
@bhanu
it will work all well if u wl write n[20] instead of *n
then it will not change the value of e2.n and wl simply print Dravid but if u wl write *n then this time its the address that would be changed so this time it wl change the value of both e1.n as well as e2.n
it will work all well if u wl write n[20] instead of *n
then it will not change the value of e2.n and wl simply print Dravid but if u wl write *n then this time its the address that would be changed so this time it wl change the value of both e1.n as well as e2.n
Samhitha said:
1 decade ago
Hi I had included string.h its giving as error. So I did as follows:
e2.age+=3;
printf("%d",e2.age); //displaying as 26
printf("%d",e1.age); //displaying as 23
Hut its not the concept of copy-constructor. Can anyone explain clearly ?
e2.age+=3;
printf("%d",e2.age); //displaying as 26
printf("%d",e1.age); //displaying as 23
Hut its not the concept of copy-constructor. Can anyone explain clearly ?
Midhun said:
1 decade ago
*n denotes Dravid
age=23
e1=Dravid,23
e2=e1 that means e2=Dravid,23
strupr(e2.n) means Dravid but using strupr used to convert Lower letter to upper letter so Dravid
again says that e2=e1
it is printing e1.n means DRAVID So op is DRAVID
age=23
e1=Dravid,23
e2=e1 that means e2=Dravid,23
strupr(e2.n) means Dravid but using strupr used to convert Lower letter to upper letter so Dravid
again says that e2=e1
it is printing e1.n means DRAVID So op is DRAVID
François said:
1 decade ago
This is what I get when trying to compile with gcc or cc
/tmp/cchEohai.o: In function `main':
test.c:(.text+0x32): undefined reference to `strupr'
collect2: ld returned 1 exit status
Do I need to add a particular standard header here?
/tmp/cchEohai.o: In function `main':
test.c:(.text+0x32): undefined reference to `strupr'
collect2: ld returned 1 exit status
Do I need to add a particular standard header here?
Venky said:
1 decade ago
Hi friends.
There is no existence of strupr () and strlwr () in standard C. So, even we add the string. H header file it will show erro in gcc compiler. Try to check man strupr. It will definitely shows there is no manual entry.
There is no existence of strupr () and strlwr () in standard C. So, even we add the string. H header file it will show erro in gcc compiler. Try to check man strupr. It will definitely shows there is no manual entry.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers