Or in other words:
We do know that
T * p;
is a pointer. Pointer holds an address. In that address is supposedly an object of type T. The following addresses
p + N*sizeof(T)
might contain other objects of type T, but we have no way to be sure (unless we can see from the code that it is so).
There is no difference between pointer to single object and pointer to array. Furthermore, a pointer can be used as an iterator, which does not "own the memory", like the other uses.
C-style string is an array of characters that contains a null character to indicate the end of the array. The string can be stored in character array that is longer than the string.
For example:
char foo[256] = "Hello"; // We fill only 6 chars: { 'H','e','l','l','o','\0' }
The ellipsis in this example can have much fun.
char *stringArray[] = {...}
1 2 3 4 5 6 7 8 9 10
|
char foo[] { "Hello" }; // local array, 6 elements, contains a C-string
char *bar = new char [6] {}; // dynamic array, 6 elements, contains a C-string (all chars are nulls)
char gaz {'F'}; // a local character, not a C-string
char *stringArray[] { foo, bar, &gaz, "world", foo+1 };
// stringArray[3] points to a C-string literal constant, which are read only
// cout << stringArray[4]; shows "ello"
// cout << *stringArray[2]; shows "F"
// cout << stringArray[2]; shows something that starts with 'F',
// but who knows how may bytes more you see before a null?
|
Obviously, you would never create such a mess; you want each pointer to have "same logical type".
Furthermore, the references to literal consts should be const:
const char * words { "Hello", "world" };