This is technically undefined behavior. However, both methods work in practice with char arrays, because the reference to str passed to scanf() turns into a pointer to the first element, which will be equal to * a pointer to the array itself (address str or &str ), therefore the same value transmitted anyway.
I'm not sure if you have worked with strings so far, but keep in mind that if you look at what is not an array, itβs easier to say that your friend method would be correct:
int myInt; scanf("%d", &myInt);
scanf() looking for a pointer, so you want to tell it the address of an integer variable. This is because passing myInt gives it a value (currently garbage), while &myInt tells where to put the value that it reads.
* Except Win16.
Michelle
source share