In the book in my avatar (this has become my new catch frase), I came across a few things in the section on pointers which they do not clearly explain. Here's an example:
I see that this is an array of characters, but why is it declared like a pointer? I don't understand how this works. Here's the full example from the book:Code:char* szString = "Randy";
Code:// DisplayString - display an array of characters both
// using a pointer and an array index
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
// declare a string
char* szString = "Randy";
cout << "The array is '" << szString << "'" << endl;
// display szString as an array
cout << "Display the string as an array: ";
for(int i = 0; i < 5; i++)
{
cout << szString[i];
}
cout << endl;
// now using typical pointer arithmetic
cout << "Display string using a pointer: ";
char* pszString = szString;
while(*pszString)
{
cout << *pszString;
pszString++;
}
cout << endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}