#include <iostream>
int main() {
using namespace std;
double wages[3] = { 10000.0, 20000.0, 30000.0 };
double (*ps)[3] = &wages;
cout << "ps = " << ps << ", *ps = " << **ps << endl;
ps = ps + 1;
cout << "ps = " << ps << ", *ps = " << **ps << endl;
double *ps2[3] = { &wages[0], &wages[1], &wages[2] };
cout << "ps2 = " << ps2 << ", *ps2 = " << **ps2 << endl;
*ps2 = *ps2 + 1;
cout << "ps2 = " << ps2 << ", *ps2 = " << **ps2 << endl;
double *ps3 =&wages[0];
double *ps4 =&wages[1];
double *ps5 =&wages[2];
double *ps6[3] = { ps3, ps4, ps5 };
return 0;
}
Here's the difference:
- Pointer to an array (
ps ): This is a pointer that points to an entire array, not just a single element. In your code, double (*ps)[3] = &wages; declares ps as a pointer to an array of 3 doubles. ps points to the entire wages array.
- Array of pointers (
ps2 ): This is an array where each element is a pointer. It can be used to create things like an array of strings, or to dynamically allocate memory for each element in the array. In your code, double *ps2[3] = { &wages[0], &wages[1], &wages[2] }; declares ps2 as an array of 3 pointers to doubles. Each element of ps2 is a pointer to a corresponding element in the wages array.
So, the main difference is that ps (pointer to an array) points to an entire array, while ps2 (array of pointers) contains individual pointers that can point to different things. |