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

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

Mahalakshmi said:   4 years ago
@Rahul Agarwal.

Perfect explanation. Thank you.

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

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


Post your comments here:

Your comments will be displayed after verification.