array of pointers - dont understand behavior

Hello,
I need help understanding pointers, in particular in regards to an array of C-strings. I will like to direct your attention to the code segments with
headings // ***** Segment 1 Below ****** and // ***** Segment 2 Below ******.
I dont understand why I am getting the outputs (lines 14 to 18) I am getting for those code segments.
Here is my code (I am using visual studio 2005)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
using namespace std;

int main()
{
    // this is an array of pointers.  Each pointer points to a c style string
    char* names4[] = {"Dog", "Daniel", "Alex"};

    cout << "names4              : " << names4 << endl;
    cout << "*names4             : " << *names4 << endl;
    cout << "names4+1            : " << names4+1 << endl;
    cout << "*(names4+1)         : " << *(names4+1) << endl;
    cout << "names4+2            : " << names4+2 << endl;
    cout << "*(names4+2)         : " << *(names4+2) << endl;

    cout << endl;
    cout << "sizeof(names4)      :  " << sizeof(names4) << " bytes" << endl;
    cout << endl;

    cout << "names4[0]           : " << names4[0] << endl;
    cout << "names4[1]           : " << names4[1] << endl;
    cout << "names4[2]           : " << names4[2] << endl;
    cout << endl;

   // ***** Segment 1 Below ******
    cout << "(names4[0]) + 1     : " << (names4[0]) + 1 << endl;
    cout << "(*names4) + 1       : " << (*names4) + 1 << endl;
    cout << endl;

    // ***** Segment 2 Below *******
    cout << "(names4[1]) + 1     : " << (names4[1]) + 1 << endl;
    cout << "(*(names4+1)) + 1   : " << (*(names4+1)) + 1 << endl;

    return 0;
}


Here is the output I get


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
names4              : 0018FF20
*names4             : Dog
names4+1            : 0018FF24
*(names4+1)         : Daniel
names4+2            : 0018FF28
*(names4+2)         : Alex

sizeof(names4)      :  12 bytes

names4[0]           : Dog
names4[1]           : Daniel
names4[2]           : Alex

(names4[0]) + 1     : og
(*names4) + 1       : og

(names4[1]) + 1     : aniel
(*(names4+1)) + 1   : aniel
Press any key to continue . . .
Last edited on
In C/C++, *(a+n) == a[n].
Furthermore, a[n] == *(a+n) == *(n+a) == n[a].
This doesn't apply to std::vectors, however.
Last edited on
rockeyboy9000
I agree with what you wrote. However I am not sure if I got my question across

If we look at cout << "(*names4) + 1 : " << (*names4) + 1 << endl;

If I understand correctly, the part in parenthesis is done first, so
(*names4) evaluates to Dog

what then happens so that Dog + 1 is evaluated as og
when you print a char * you keep printing characters till you find a '\0';
char* names4[]; //char ** names4 So doing a dereferencing gives you a char *.
(*names4) does not evaluate to Dog but to a pointer that points to the D, if you increment that pointer then it will point to the o.
Topic archived. No new replies allowed.