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

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.

Raju said:   9 years ago
My GCC compiler gets closed after running this program. Why its happens, anyone helps me.

Pradeep shukla said:   10 years ago
Error in GCC compiler ----> undefined reference to 'strupr'.

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".

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.

Ratan said:   1 decade ago
The program has error since no <string.h> header file is included and string functions are being used.

Rahul said:   1 decade ago
Is it possible to assign object of structure in C?

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

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.

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 ?


Post your comments here:

Your comments will be displayed after verification.