This is a C-style string, which means that it's really an array of characters with a null (ASCII code 0) character at the end of the string to indicate that it's the end (the compiler puts it in for you when it sees the literal).
The character pointer points to the first character in the array (this is because arrays decay into pointers whenever the context demands it).
Pointers can have the p[i]
subscript syntax applied to them, which is identical to saying *(p + i)
. This is a common idiom for accessing the i
th element of an array given a pointer to the first element.
Using these facts, you can iterate through all the characters in the array this way:
// '\0' is the null character
for (int i = 0; aCharPointer[i] != '\0'; ++i) {
// Do something with aCharPointer[i]
}
or this way:
for (char *p = aCharPointer; *p != '\0'; ++p) {
// Do something with *p
}
You can also call a function that requires a character pointer to a C-style string:
#include <cstring>
size_t lengthOfString = strlen(aCharPointer);
You can, of course, also manually dereference the pointer:
assert(*aCharPointer == 'A');