C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Point Out Errors (Q.No. 11)
11.
Point out the error in the program?
#include<stdio.h>

int main()
{
    struct emp
    {
        char name[25];
        int age;
        float bs;
    };
    struct emp e;
    e.name = "Suresh";
    e.age = 25;
    printf("%s %d\n", e.name, e.age);
    return 0;
}
Error: Lvalue required/incompatible types in assignment
Error: invalid constant expression
Error: Rvalue required
No error, Output: Suresh 25
Answer: Option
Explanation:

We cannot assign a string to a struct variable like e.name = "Suresh"; in C.

We have to use strcpy(char *dest, const char *source) function to assign a string.

Ex: strcpy(e.name, "Suresh");

Discussion:
14 comments Page 1 of 2.

Preethi said:   1 decade ago
What is this Lvalue error? I have seen it many time.

What does it mean actually?.

Can anyone explain me please ?

Afiz said:   1 decade ago
Lvalue : left value, but this answer is wrong B is corrct

Sam910 said:   1 decade ago
@afiz - why is it B?

There is no problem in the structure assignment

Sowmya said:   1 decade ago
can someone explain tis concept ?

Hemant said:   1 decade ago
Can anyone explain this, why can't we assign an array of character with string ?

Rajesh.T.K. said:   1 decade ago
In Find output of the program section, we have a question in which string is assigned. Why not here?

Alex said:   1 decade ago
We can't assign an array of character, because in this one char should go on each index that can not be possible by directly assigning.

For that we must use strcpy(e.Name, "Suresh");

Mythili said:   1 decade ago
Can it be assigned as char name[25] = "suresh";?
(1)

Ravina said:   10 years ago
Can we write e.name={"Suresh"}?

DileepChandra said:   10 years ago
Lvalue means left side value and here we are using string by declaring char. So there by it asks Lvalue.


Post your comments here:

Your comments will be displayed after verification.