P
Paul
There seems to have been much confusion about pointer to array types in C++.
Ahem many C++ experts seem to think the following is true:
/*Very wrong and very bad stuff here*/
int arr[6];
int (*p)[6]= &arr;
p[2] = 56;
/*Very wrong and very bad stuff here*/
The above seems to be the opinion of many of the *experts* in this
newsgroups of how to use pointers to arrays in C++ but this is very bad and
very wrong.
A pointer to an array of 1 dimension is always of base type of the array,
for example int*. The above code should be done like this:
int arr[6];
int* p= arr;
p[2] = 56;
A pointer of type int(*p)[6], is a pointer to a 2 dim array like so:
int arr[3][6];
int (*p)[6] = arr;
This is NOT a pointer to an array of int[6].
It has to be this way because when we index and array with the subscript the
array is converted to a pointer+index and then dereferenced.
With:
int arr[6];
arr[2] = 0;
The value returned from the dereferenced pointer must be an int type, it is
not an array type.
So remember a pointer to an array is always n-1 dim less than the array it
points to.
Please refer to your ISO C++ standards for more clarification.
TY and HTH.
P.S. I can't believe so many *experts* don't understand these basics.
Ahem many C++ experts seem to think the following is true:
/*Very wrong and very bad stuff here*/
int arr[6];
int (*p)[6]= &arr;
p[2] = 56;
/*Very wrong and very bad stuff here*/
The above seems to be the opinion of many of the *experts* in this
newsgroups of how to use pointers to arrays in C++ but this is very bad and
very wrong.
A pointer to an array of 1 dimension is always of base type of the array,
for example int*. The above code should be done like this:
int arr[6];
int* p= arr;
p[2] = 56;
A pointer of type int(*p)[6], is a pointer to a 2 dim array like so:
int arr[3][6];
int (*p)[6] = arr;
This is NOT a pointer to an array of int[6].
It has to be this way because when we index and array with the subscript the
array is converted to a pointer+index and then dereferenced.
With:
int arr[6];
arr[2] = 0;
The value returned from the dereferenced pointer must be an int type, it is
not an array type.
So remember a pointer to an array is always n-1 dim less than the array it
points to.
Please refer to your ISO C++ standards for more clarification.
TY and HTH.
P.S. I can't believe so many *experts* don't understand these basics.