Zhou said:
Yes, by this changing, it work properly. But I still do not
understand. Could you kindly give a brief explanation?
Also I tried to print all the 4 numbers and I followed this way, I
find I also need:
nPtr[ 0 ] = array[ 0 ];
and I checked array[ 0 ] is just the same as &array[ 0 ][ 0 ] by
printf( "%p\n%p\n", array[ 0 ], &array[ 0 ][ 0 ] );
printf( "%p\n%p\n", array[ 1 ], &array[ 1 ][ 0 ] );
Could you please kindly give me a explanation?
What is array[ 0 ] actually is? Is it an array which contain the the
first row of this array? Or is it a pointer, with the address of array[
0 ][ 0 ]. Or internally it is both?
The full explanation is below.
If I define such an array of pointers, nPtr[ 2 ], I need to initialize
all the pointers in this array,right?
Right.
Is it possible to define a single array which can point to any
address of a 2-D array?
Well, you got one answer on how to get a segment of the array, now for
the full explanation:
An 2D array (as you have allocated) is not an array of pointers to an
array of elements, it is in fact a bunch of elements allocated in one
chunk.
array is of type int[][2] and is convertible to a pointer to an array of
two elements (int (*)[2]). You can declare a variables of this type
like this:
// Get from zeroth 'row' on down
int (*var0)[2] = array; // or
int (*var0)[2] = array+0; // or
int (*var0)[2] = &array[0];
// Get from first 'row' on down
int (*var1)[2] = array+1; // or
int (*var1)[2] = &array[1];
Note: not int *var[2] as this is an array of int pointers of two
elements. Note also that you cannot cast to or define this type this
directly, something in the language/parser prevents this. To
cast/return, you need to generate a typedef like so:
typedef (*ptrToTwoElementArray_t)[2];
(something that I just figured out now, thanks to you. That has
been bugging me forever, as I've never been able to return or cast
to something like this. Thanks very much!)
array[0] is of type int[] and is convertible to a pointer to an array of
elements (int *). You can also declared a variable of this type
like this:
// Get zeroth 'row'
int *var0 = *(array);
int *var0 = *(array+0);
int *var0 = array[0];
// Get first 'row'
int *var1 = *(array);
int *var1 = *(array+1);
int *var1 = array[1];
This is why your code:
printf( "%p\n%p\n", array[ 0 ], &array[ 0 ][ 0 ] );
printf( "%p\n%p\n", array[ 1 ], &array[ 1 ][ 0 ] );
gave you the results that you found.
array[0][0] is of type int and is convertible to an int. You can also
declare a variable of this type like this:
int var = array[0][0];
This should be obvious.
When you dereference an array (declared as you described) using the []
operators, the compiler knows that this is not an array of pointers to
an array of ints, but that it is a big blob of ints and that it can
calculate the offset to the ints via the formula x+y*MaxX.
(I need to ask such silly quetions...but I really know nothing and
cannot find a book talk about these)
Don't think your question silly. I've been trying to figure this out
for 20 years.
The only silly question is the unasked one. I
certainly learned something from answering this question, I hope that
I explained it sufficiently for you to too.
Adrian