I got this question... after a brief (hour) time skimming through C++ for dummies (I highly don't recommend, for extreme newbies I mean, even after the year or so of off and on programming, it is still a bit difficult from time to tim e (used to be all the time, but it has gradualy got better) to understand Stephen Randy Davis [if he wrote it or not, I have no proof] explanations of certain topics.), I started reading what it had to say in the chapter for pointers. I came across using poitners to access arrays. Well, I got it to work, but Stephen writes that it is more common to see pointers to display arrays then just displaying arrays... example:
Code:
// DisplayArray example 1:
void DisplayArray(int intArray[], int nSize)
{
int * pArray = intArray;
cout << "\n\tThe value of the array is:\n";
for (int i = 0; i < nSize; i++, pArray++)
{
cout << i << " : " << *pArray << "\n";
}
cout << endl;
}
Code:
// DisplayArray example 2:
void DisplayArray(int intArray[], int nSize)
{
cout << "\n\tThe value of the array is:\n";
for (int i = 0; i < nSize; i++)
{
cout << i << " : " << intArray[n] << "\n";
}
cout << endl:
}
Code:
// This block code of main goes for both codes shown above...
int main(int nNumberofArgs, char* pszArgs[])
{
int array[] = {4, 3, 2, 1};
DisplayArray(array, 4);
return 0;
}
I can see here that it just does the same thing... but Davis says/writes this:
Quote:
You may think this pointer conversion is silly; however, the pointer version of displayArray() is actually more common among C++ programmers in the know than the array version. For some reason, C++ programmers don't seem to like arrays.
The use of pointers to access arrays is nowhere more common than in accessing of character arrays.
|
m'kay. Telling me it's more common to see the pointer display function then just then using the array function example, doesn't help me much (if not at all). Davis then goes on to talk about pointer operations to a string... I fail when it comes to trying to figure situations in c++(not all c++ code was like this to me, I picked up some operators/usefull methods of operating code almost immediatly) where an operator or piece of code is usefull to me. I guess it would be ignorant of me to say that he should have stayed on topic and discussed this a little more, I probably wasn't ready for this if I didn't already see what he was trying to explain to the reader, something along the lines of "other ways you can use this pointer array method to your advantage" would have worked to help the reader a bit more on the topic at hand IMO. He may have said this somewhere else in the book (more then likely not before this chapter, for I (re-)read the first good portion of the book several times to grasp the concept of c++ programming. If he does, well, to me it seems as though it would cause confusion to someone (no proof other then me) who gets frustrated trying to think of a situation where it could come in handy. I know how to work pointers (* &), I'm not completely ignorant when it comes to that, but like many things, I may not fully understand it's power, and maybe Davis is assuming I know what he is talking about. Anyways, I don't know, so, can someone give me a good situation where this could be extremely usefull, maybe a piece of code that might head me in the direction of Davis? Thank you, and I'll be back later on this morning to maybe clear up my question some more, since I am not exactly in the state of having foggless thoughts.