Re: How to define a pointer to multiple-subscripted arrays



Adrian Hawryluk <adrian.hawryluk-at-gmail.com@xxxxxxxxxx> writes:

Zhou Yan wrote:
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

Thank you very much for you explanation. I will print this message
out to read a few times to get the whole idea...

Thanks!
.



Relevant Pages

  • Re: K&R2 Secition 5.9 - major blunders
    ... Each element of b doesn't point to a 20 element array of int. ... This mistake is crucial because ... my explanation is really the qualities of something else: ... > The use of the array of pointers is to store the strings. ...
    (comp.lang.c)
  • Re: K&R2 Secition 5.9 - major blunders
    ... Each element of b doesn't point to a 20 element array of int. ... This mistake is crucial because ... my explanation is really the qualities of something else: ... > The use of the array of pointers is to store the strings. ...
    (comp.lang.c)
  • Re: How to define a pointer to multiple-subscripted arrays
    ... Could you please kindly give me a explanation? ... Is it an array which contain the the ... > If I define such an array of pointers, nPtr, I need to initialize ... When you dereference an array 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. ...
    (comp.lang.c)
  • Re: Using INDIRECT & R1C1 Ref style
    ... Your explanation was far superior. ... Valko" wrote: ... That expression will return an array of either TRUE or FALSE. ... multiple sheets and I'd rather not have to modify the range name for ...
    (microsoft.public.excel.worksheet.functions)
  • Re: extract matching vales
    ... Here is an explanation I wrote for another poster. ... However, if you drag copy down, once the data that meets the criteria is ... I used a "psuedo" error trap that effectively does the same thing but is ... This will return an array of TRUE's or FALSE's. ...
    (microsoft.public.excel.misc)

Loading