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 1 of 6.
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.
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
#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
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;
}
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)
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.
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)
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
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.
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)
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.
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.
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.
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.
Pal chakraborty said:
1 decade ago
It is an example of side effect of copy constructor.
Suppose
e1.n=100;
e2.n=23;
where 100 is a location where the string Dravid starts.
when we write e2=e1 then
e2.n=100;
e2.age=23;
so when strupr(e2.n) or strupr(100) then it capitalizes the string at location 100.as both e1.n and e2.n acess the location 100 so both capitalizes instead of (e2.n);
Suppose
e1.n=100;
e2.n=23;
where 100 is a location where the string Dravid starts.
when we write e2=e1 then
e2.n=100;
e2.age=23;
so when strupr(e2.n) or strupr(100) then it capitalizes the string at location 100.as both e1.n and e2.n acess the location 100 so both capitalizes instead of (e2.n);
Robert said:
9 years ago
If you do the above operations in gcc:
*If you assign e2 = e1, e2 will COPY the contents of the structure e1, but e2 will exist independently of e1.
* Whatever changes you will do to either e2 or e1 after that doesn`t impact both. So, if you change variable age in e1 won`t affect the variable age in e2.
* There is no such strupr in string.h.
*If you assign e2 = e1, e2 will COPY the contents of the structure e1, but e2 will exist independently of e1.
* Whatever changes you will do to either e2 or e1 after that doesn`t impact both. So, if you change variable age in e1 won`t affect the variable age in e2.
* There is no such strupr in string.h.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers