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

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.
(5)

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.
(5)

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;
}
(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.
(1)

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

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.

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

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.

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

Undefined reference to 'main'.

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.


Post your comments here:

Your comments will be displayed after verification.