score:3

Accepted answer

they accomplish the same thing. buf evaluates to a pointer to the first element (int*) of the array, while &buf gets you a pointer-to-array-of-8-int*s. (the types differ in a somewhat obscure way.) both of those pointers can be used to access the byte representation of the array via casting to char*, which is what memset() conceptually does. internally, you're just passing the same address in both cases in practice.

as a pedantic note, it's not guaranteed that the internal representation of a null pointer is as all-zero bytes (though this is extremely likely in practice).

to directly declare a variable of type pointer-to-array-of-8-int*s, you could use the following by the way:

int* buf[8];
int* (*bufp)[8] = &buf; /* initialize a pointer-to-array-of-8-int* */

bufp is different from a pointer to the first element of the array in that you can do e.g. sizeof *bufp to get the size of the array. (that information would not be available to the compiler with just a pointer to the first element. the above makes it inherent in the type.)

as seen above, pointers and arrays aren't the same thing (they have different types, and there are dedicated types for arrays). the reason people often confuse pointers and arrays is that arrays usually evaluate to a pointer to their first element. sizeof is one case where that does not happen. if the array degraded to a pointer with sizeof, then you'd get the size of that pointer instead of the size of the array, which wouldn't be useful.


Related Query

More Query from same tag