#include <stdio.h>
struct Student {
char name[10];
int age;
double gpa;
};
int main(void)
{
struct Student s1;
s1.age = 20;
s1.gpa = 4.3;
s1.name[10] = "Tom";
printf("%s", s1.name);
return 0;
}
I know 'strcpy' function or other way to assign string. but Why doesn't it work above code..? Please help me.
Specially, s1.name[10] = "Tom";
Thanks.
s1.namehas only 10 elements starting froms1.name[0], sos1.name[10]is out-of-range and you must not access (no read nor write) there.char name[10] = "Tom";works because it declares and initializes the array. Butchar name[10]; name = "Tom";won't work because arrays cannot be assigned.s1.name = "Tom";might make some sense in a future rev of C, but not today.