Can a pointer be indexed like array?

A pointer is normally considered an atomic data type. Therefore, a pointer cannot "be indexed like an array" in that it is not possible to access portions of a pointer through an index.

However, a pointer can point to data which can be accessed through pointer operations as well as through index operations. Further, multiple pointers can be arranged in an array of pointers, and each individual pointer within that array of pointers can be reached through its array index.

Example 1: an array of pointers

const char* const array_of_pointers[] = {

"Hello, World", "Hello, Venus", "Hello, Mars"


In this example, each of array_of_pointers[0], array_of_pointers[1] or array_of_pointers[2] evaluates to one pointer, selected with the array index 0..2.

Example 2: access to data through pointer using an array-style index

void example(char* const text) {

*text = 'a'; // set first character pointed to to 'a'

*(text+1) = 'b'; // second char becomes 'b'

// the following are equivalent to the above, but use index notation:

text[0] = 'a';

text[1] = 'b'; }

Key to example 2 is the declaration of the pointer as a constant pointer.