I’ve bought a beginners book for visual c++ and have come the chapter which involves arrays, strings and pointers. I understand the concept, but when it comes to multidimensional arrays I got a bit lost.
Array and and pointer declaration:
double beans[3][4];
double* pbeans;
I understood this part:
*You can declare and assign a value to the pointer pbeans, as follows:
double* pbeans;
pbeans = &beans[0][0];
But when the author presents that you can assign the address for the first row/dimension by typing this statement he lost me:
pbeans = beans[0];
Why can we skip the “Address-Of” operator here?
To me the logical thing would be:
pbeans = &beans[0];
Because an array decays to a pointer to the first element. If we have an array called
arr, then any usage ofarrwhere an array won’t fit will generally result inarrbeing treated as a pointer. This is called decay.In this case,
beans[0]is an array; to be specific, andouble[4]. However, as you cannot assign an array to a pointer, the array decays into adouble*.The code you provided,
double* pbeans = &beans[0];would be trying to assign a pointer to an array of doubles to a pointer to a double; that won’t work due to a type mismatch.Also, all that aside, this doesn’t sound like a very good book; teaching arrays, strings and pointers together smells of C.