I'm new to C. The string assignment in the following code works:
#include<stdio.h>
int main(void){
char str[] = "string";
printf("%s\n",str);
}
But doesn't work in the following,even I give the index number to the name[]:
#include <stdio.h>
int main(void){
struct student {
char name[10];
int salary;
};
struct student a;
a.name[10] = "Markson";
a.salary = 100;
printf("the name is %s\n",a.name);
return 0;
}
Why does this happen?
char name[10];, you get an array with ten elements, numbered 0 to 9 inclusive. Soa.name[10]is outside the bounds of the array. It's not a good idea to try to assign it a value.