I prepared this simple program in hopes that it will help explain pointers to a 2nd semester C++ programming class. Do you see anything inaccurate or particularly confusing in any of my comments?
I'm a little worried about this line in particular:Anything wrong with that?Code:// It also stores an int** pointer to the 2-d array itself, // which holds the beginning address of the block of // int* pointers.
Thanks for looking.
Code:#include <iostream> using namespace std; int main () { int i,j; int iarr[4][2] = {{1,2},{3,4},{5,6},{7,8}}; // When we declare a 2-dimensional int array, the compiler: // allocates a contiguous block of memory in which to // store the requested number of ints. // It also allocates another contiguous block of memory // NO // in which it stores int* pointers to the 1-dimensional // arrays representing the "rows" (the last dimension) // of the int array. // It also stores an int** pointer to the 2-d array itself, // NO // which holds the beginning address of the block of // int* pointers. // code deleted ... return 0; }



LinkBack URL
About LinkBacks




The general idea that arrays are "the same" as pointers was the misconception that got me here in the first place. By the time you finish qualifying it to cover the exceptions, it almost seems that the "general" statement is just a special case. Good point about the addresses, but doesn't that in itself disprove the "general" statement? And if you pass an array to a function by a pointer, what the function gets IS a pointer. I don't see anything paradoxical about that. I wouldn't expect the function to know anything other than the information that a pointer is supposed to convey -- a type and an address.