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 I am getting for those code segments.
Here is my code (I am using visual studio 2005)
Here is the output I get
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 I am getting for those code segments.
Here is my code (I am using visual studio 2005)
Rich (BB code):
#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;
}
Rich (BB code):
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 . . .