Re: Array pointers

Jens.Toerring_at_physik.fu-berlin.de
Date: 08/16/04


Date: 16 Aug 2004 18:46:09 GMT

Erik Max Francis <max@alcyone.com> wrote:
> Jens.Toerring@physik.fu-berlin.de wrote:

>> A two-dimensional array like your
>>
>> int array[2][3];
>>
>> doesn't decay to a pointer to pointer to int but just a pointer to
>> int.

> No, it decays to an int *[3].

Sorry, yes you are correct (and using 'array' directly will still
results in a compiler warning). But what I really wanted to express
is that a two-dimensional array isn't a pointer-to-pointer. And
a more-dimensional array is in C basically a flat array with all
rows stored one after each other in memory. So, if you want to
write a function that prints two-dimensional int matrices of
arbitrary rank you must pass the function a pointer to the first
element, i.e. &array[0|[0] or array[0], plus the dimensions and
treat it in the function as a pointer to a flat array, using the
additional knowledge about how the elements are stored. Passing
the function 'array' itself usually works also since the resulting
pointer is typically the same address as that of the first element
of the array.

>> > and then when I run the binary, I get a segmentation fault.
>>
>> Yes, that was to be expected...

> Your proposal would also fail.

Why?
                                   Regards, Jens

-- 
  \   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
   \__________________________  http://www.toerring.de


Relevant Pages

  • Re: Warning on assigning a function-returning-a-pointer-to-arrays
    ... This declares pfunc as a function taking no arguments and returning ... int x, y; ... Presumably pfuncwill return a pointer to a single int, ... or the first of a sequence of "array 5 of int"s. ...
    (comp.lang.c)
  • Re: Question about pointers and multi-dimension arrays
    ... I should fetch the address which p contains and find an int value ... 0xbfd9e880 contains the int pointer 0xbfd9e880. ... "pointer to array of two arrays of two ints", ... the first element of array2d is still ...
    (comp.lang.c)
  • Re: Newbie
    ... to talk about the int value 3 and the int value 4, ... It also lets you talk about pointer ... C has a special rule for array objects. ... to printf() is: ...
    (comp.lang.c)
  • Re: union {unsigned char u[10]; ...}
    ... But character type is not a union. ... u.a is of type int. ... has to do so to make pointer equality work consistently). ... were a single-element array. ...
    (comp.lang.c)
  • Re: Evaluating unary *
    ... > expression above was to convert a pointer to int ... The result is a pointer to the array that would follow the ... The result is and 'int *' values pointer to the first element just ...
    (comp.std.c)