When you try and assign character arrays after initializing them you must use a function like strcpy.
#include<stdio.h>
void main()
{
struct emp
{
char name[25];
int age;
float bs;
};
struct emp e;
e.name = "Steven";
e.age = 25;
printf("%s %d\n", e.name, e.age);
}
This code would only work if I made the following changes
#include<stdio.h>
void main()
{
struct emp
{
char name[25];
int age;
float bs;
};
struct emp e;
strcpy(e.name, "Steven");
e.age = 25;
printf("%s %d\n", e.name, e.age);
}
However, if we use a pointer instead of an array we can assign the string after initializing.
#include<stdio.h>
void main()
{
struct emp
{
char *name;
int age;
float bs;
};
struct emp e;
e.name = "Steven";
e.age = 25;
printf("%s %d\n", e.name, e.age);
}
Why does this work but not the first code if both a pointer and an array point to a memory location?