In your reverseCString function, you never initialize the address pHead should be pointing to.
Line 39 is a bit odd...
*pHead
will be dereferencing so random location since you never initialized pHead. The compiler should spit out an error. It should also complain with
*sentence[0]
. Think of the array this way: sentence, the name of the array, is actually a pointer to the first item, or sentence[0]. The square brackets are a way to dereference the pointer by some offset, so
sentence[0]
will already return the first value in the array. Attempting to dereferencing a char will not work.
If you want pHead to
point to the first item in sentence, merely assign the memory address stored in sentence, i.e.
pHead = sentence
. Now you can do either of the following to access the characters:
1 2 3 4 5 6 7 8 9 10 11
|
char* pTail = sentence + 19; //Initialize pTail with the address of sentence's last char
//Note that this and pTail = sentence + 19 are the same thing.
//Using sentence here because pTail is not really necessary
for(int I = 20; I > 0; I--)
std::cout << sentence[I-1]; // operator[]
//or
for(int I = 20; I > 0; I--)
std::cout << *(sentence + I - 1); // operator*
//with pTail
for(int I = 0; I < 20; I++)
std::cout << *(pTail - I);
|
This code has not been tested, so it probably has bad style and bugs.
Edit: Fixed up my example code because I can't stand the nub mistakes I made.