structure

the following code gives a compilation error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #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;
}

wheras the following code doesnt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>

int main()
{
    struct emp
    {
        char name[25];
        int age;
    };
    struct emp e[2];
    int i=0;
    for(i=0; i<2; i++)
        scanf("%s %d ", e[i].name, &e[i].age);

    for(i=0; i<2; i++)
       printf("%s %d ", e[i].name, e[i].age);
    return 0;
}

You are right.
There is no question in your post.

If your question is "why am I getting an error", the reason is line 12. You can't assign a C-string like that. Use std::string instread.
Topic archived. No new replies allowed.