I am trying to assign a Character array in struct using a string literal as shown below but it is not working:
s1.name[20] = "Mark";//prints some garbage string with special symbols
But when I tried assigning it using strcpy its working fine. Could someone explain me why it is failing in the 1st case?
strcpy(s1.name, "Mark");//This Works
Below is the complete code:
struct student {
char name[20];
int id;
float marks;
};
int main(int argc, const char * argv[])
{
struct student s1;
s1.name[20] = "Mark";
//strcpy(s1.name, "Sonoo Jaiswal");
s1.id = 22;
s1.marks = 76;
printf("%s: , %d: , %f: \n",s1.name, s1.id,s1.marks);
}