It is technically undefined behaviour. However, both methods work in practice with char arrays because the reference to str
being passed to scanf()
turns into a pointer to the first element, which will be equal* to a pointer to the array itself (the address of str
, or &str
), so the same value gets passed either way.
I'm not sure whether you've only been working with strings so far, but bear in mind that if you look at something that's not an array, it's easier to tell that your friend's method would be correct:
int myInt;
scanf("%d", &myInt);
scanf()
is looking for a pointer, so you want to give it the address of your integer variable. This is because passing myInt
gives it a value (currently garbage), whereas &myInt
tells it where to put the value that it reads.
*Except on Win16.