Interview Questions

Since array references decay into pointers ...

C Interview Questions and Answers


(Continued from previous question...)

Since array references decay into pointers ...

Q: Since array references decay into pointers, if arr is an array, what's the difference between arr and &arr?

A: The type.
In Standard C, &arr yields a pointer, of type pointer-to-array-of-T, to the entire array. (In pre-ANSI C, the & in &arr generally elicited a warning, and was generally ignored.) Under all C compilers, a simple reference (without an explicit &) to an array yields a pointer, of type pointer-to-T, to the array's first element.
For a simple array
int a[10];
a reference to a has type ``pointer to int,'' and &a is ``pointer to array of 10 ints.'' For a two-dimensional array like
int array[NROWS][NCOLUMNS];
a reference to array has type ``pointer to array of NCOLUMNS ints,'' while &array has type ``pointer to array of NROWS arrays of NCOLUMNS ints.''

(Continued on next question...)

Other Interview Questions