How do I declare a pointer to an array?

by Vijayaprasad 2010-02-16 10:05:34

Usually, you don't want to. Consider using a pointer to one of the array's elements instead. Arrays of type T decay into pointers to type T, which is convenient; subscripting or incrementing the resultant pointer access the individual members of the array. True pointers to arrays, when subscripted or incremented, step over entire arrays, and are generally only useful when operating on multidimensional arrays, if at all. (See question 22 above.) When people speak casually of a pointer to an array, they usually mean a pointer to its first element.

If you really need to declare a pointer to an entire array, use something like "int (*ap)[N];" where N is the size of the array. (See also question 66.) If the size of the array is unknown, N can be omitted, but the resulting type, "pointer to array of unknown size," is useless.

Tagged in:


You must LOGIN to add comments