It sounds like you have several questions:
- calling
scanf("%s", name)
should have given an error, since %s
expects a pointer and name
is an array? But as others have explained, when you use an array in an expression like this, what you always get (automatically) is a pointer to the array's first element, just as if you had written scanf("%s", &name[0])
.
- Having
scanf
write into name
should have given an error, since name
was initialized with a string constant? Well, that's how it was initialized, but name
really is an array, so you're free to write to it (as long as you don't write more than 10 characters into it, of course). See more on this below.
- Characters got copied around, even though you didn't call
strcpy
? No real surprise, there. Again, scanf
just wrote into your array.
Let's take a slightly closer look at what you did write, and what you didn't write.
When you declare and initialize an array of char
, it's completely different than when you declare and initialize a pointer to char
. When you wrote
char name[10] = "yasser";
what the compiler did for you was sort of as if you had written
char name[10];
strcpy(name, "yasser");
That is, the compiler arranges to initialize the contents of the array with the characters from the string constant, but what you get is an ordinary, writable array (not an unwritable, constant string constant).
If, on the other hand, you had written
char *namep = "yasser";
scanf("%s", namep);
you would have gotten the problems you expected. In this case, namep
is a pointer, not an array. It's initialized to point to the string constant "yasser"
, which is not writable. When scanf
tried to write to this memory, you probably would have gotten an error.