Question

Could anyone explain the difference between these lines?
Where: char ** d_array, I just can't predict the outcome of these lines. Never really understood the pointers in C++, and didn't find any tutorial where they'd explain such lines yet.

Thanks in advance!

• d_array + 2
• *(d_array+ 2)
• *(d_array+ 2) + 1
• *(*(d_array+ 2) + 1)
• d_array[1][1]
• *(d_array[1]+1)
Was it helpful?

Solution

#include <iostream>
using namespace std;

int main() {
    const char* d_array[] = { "one", "two", "tree", "four" };

    cout << "1: " <<  d_array + 2 << endl;
    cout << "2: " <<  *(d_array+ 2) << endl;
    cout << "3: " <<  *(d_array+ 2) + 1 << endl;
    cout << "4: " <<  *(*(d_array+ 2) + 1) << endl;
    cout << "5: " <<  d_array[1][1] << endl;
    cout << "6: " <<  *(d_array[1]+1) << endl;
}

Output:

1: 0xbfee7208
2: three
3: hree
4: h
5: w
6: w

http://ideone.com/XjVYsg to see it in action

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top