*a
is same as a[0]
, pointing to the first element of a
. Generally, *(a + i)
is same as a[i]
.
I don't think you want an array of string
s (string
is a typedef
of char*
) all pointing to the same variable (and it's dangerous to use %s
with &b
, as you don't know where the next zero byte is!), I assume you want an array of char
instead (don't forget space for the null terminator then)
Edit: Strings in C
Strings in C are a bunch of non-zero characters (the value, not the digit '0'
found in ASCII which is 48
) ended by a zero byte 0
(the null terminator '\0'
is a 0
cast to char
), stored contiguously somewhere in memory. If a function like printf
expects a string, it expects the memory address of the first of those characters, and will likely read from there until it hits a '\0'
, assuming everything including that final byte to be part of the string. So when you pass &b
for a string, it will also process the byte next to it, and if that is not zero, will print even more, potentially giving out secrets, or triggering segmentation faults (which might be the lesser evil).
As there are padding bytes inserted to have all variables start at a memory address that's a multiple of their size, and those are initially zero in most case, such mistakes sometimes remain hidden in simple tests, as printf
will take a padding byte for the null terminator.
With functions like scanf
, there are even more potential issues (buffer overflows), unless you specify the maximum length you provide space for. A single char
could take a string of zero characters (empty string) only.