I recently embarrassed myself while explaining to a colleague why
char a[100];
scanf(\"%s\", &a); // notice a & in front of \'a\'
is ve
A C array can be implicitly converted to a pointer to its first element (C99:TC3 6.3.2.1 §3), ie there are a lot of cases where a
(which has type char [100]
) will behave the same way as &a[0]
(which has type char *
). This explains why passing a
as argument will work.
But don't start thinking this will always be the case: There are important differences between arrays and pointers, eg regarding assignment, sizeof
and whatever else I can't think of right now...
&a
is actually one of these pitfalls: This will create a pointer to the array, ie it has type char (*) [100]
(and not char **
). This means &a
and &a[0]
will point to the same memory location, but will have different types.
As far as I know, there is no implicit conversion between these types and they are not guaranteed to have a compatible representation as well. All I could find is C99:TC3 6.2.5 §27, which doesn't says much about pointers to arrays:
[...] Pointers to other types need not have the same representation or alignment requirements.
But there's also 6.3.2.3 §7:
[...] When a pointer to an object is converted to a pointer to a character type, the result points to the lowest addressed byte of the object. Successive increments of the result, up to the size of the object, yield pointers to the remaining bytes of the object.
So the cast (char *)&a
should work as expected. Actually, I'm assuming here that the lowest addressed byte of an array will be the lowest addressed byte of its first element - not sure if this is guaranteed, or if a compiler is free to add arbitrary padding in front of an array, but if so, that would be seriously weird...
Anyway for this to work, &a
still has to be cast to char *
(or void *
- the standard guarantees that these types have compatible representations). The problem is that there won't be any conversions applied to variable arguments aside from the default argument promotion, ie you have to do the cast explicitly yourself.
To summarize:
&a
is of type char (*) [100]
, which might have a different bit-representation than char *
. Therefore, an explicit cast must be done by the programmer, because for variable arguments, the compiler can't know to what it should convert the value. This means only the default argument promotion will be done, which, as litb pointed out, does not include a conversion to void *
. It follows that:
scanf("%s", a);
- goodscanf("%s", &a);
- badscanf("%s", (char *)&a);
- should be ok