multidimensional array = pointer to pointer ?

Discussion in 'C Programming' started by Kobu, Jan 19, 2005.

  1. Kobu

    Kobu Guest

    I've read the FAQ and several posts on multidimensional arrays and how
    their names decay to pointer to arrays (not pointer to pointers).

    If this is so, why does the following code fragment compiler and run
    correctly?:


    #include <stdio.h>

    int main()
    {
    int example[4][4]={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
    15, 16};

    printf("\nBEFORE DOUBLE DEREFERENCING of example:\n");
    printf("\nexample[0][0] = %d\n",example[0][0]);


    **example = 100; /* double dereferencing 'example' */


    printf("\nAFTERDOUBLE DEREFERENCING of example:\n\n\n");
    printf("\nexample[0][0] = %d\n",example[0][0]);

    return 0;
    }

    The code ('example') behaves just like a pointer to a pointer. Then
    why isn't it a pointer to a pointer?
     
    Kobu, Jan 19, 2005
    #1
    1. Advertising

  2. Kobu

    Kobu Guest

    Is this a logical explanation for what I've posted (based on pointer to
    array)? ...

    1) 'example' points to example[0]

    2) *example is example[0]
    (and example[0] is another array name, which points to example[0][0])

    3) so *(*example) = *(example[0]) = example[0][0]



    I see how that works, except for the fact that *example being equal to
    example[0], to me, means the physical array example[0], not the just
    the name.
    Can anyone explain 2) and how the result of *example can be once again
    treated as another array name?



    With 'ragged' arrays, the above is much more concrete because there is
    an intermediate array of pointers to other arrays (so double
    dereferencing takes 2 specific paths to the final destination object).

    How can double dereferencing take 2 paths with a conventional array?
    Some help would be appreciated
     
    Kobu, Jan 19, 2005
    #2
    1. Advertising

  3. Kobu

    Chris Torek Guest

    In article <>,
    Kobu <> wrote:
    >I've read the FAQ and several posts on multidimensional arrays and how
    >their names decay to pointer to arrays (not pointer to pointers).


    Yes.

    >If this is so, why does the following code fragment compile and run
    >correctly?:


    Learn to love The Rule :) See <http://web.torek.net/torek/c/pa.html>
    et seq.; pay attention to the distinction between "object" and "value"
    and what happens when you put an object in a value context.

    [snippage]
    >int example[4][4]={ /*...*/ };


    Here "example" is an object of type "array 4 of array 4 of int".

    >**example = 100; /* double dereferencing 'example' */


    The inner "*example" needs the "value" of "example", so apply The
    Rule, obtaining a pointer to its first element. This first element
    has type "array 4 of int", so the pointer has type "pointer to
    array 4 of int".

    The unary "*" operator then follows the pointer, obtaining the
    entire "array 4 of int" (example[0][0] through example[0][3]
    inclusive). This is an object of type "array 4 of int".

    Now we have *(that), so we need the value of that. Once again,
    we need the "value" of an array object, so apply The Rule. The
    result is a value of type "pointer to int" pointing to the first
    element of the entire "array 4 of int". The unary "*" operator
    then follows that pointer, obtaining the (single) int to which
    it points.

    If you look at the figure in <http://web.torek.net/torek/c/pa.html>,
    the two steps here are "find the red circle" and then "find the
    black circle". Had you written, say:

    *(*(example + 2) + 1)

    the steps would be: obtain a red circle, move it forward two units
    (two "full red circles" worth of distance), then obtain a black
    circle within the moved-down red circle, then move *it* -- the
    smaller, black circle -- forward one unit.

    This is how indexing works in C, and how it is that arrays and
    pointers *work* similarly (by obtaining intermediate pointer values
    that are not stored anywhere in C's regular object-storage memory).
    You can, of course, make your own intermediate pointers and store
    them in memory.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, Jan 19, 2005
    #3
  4. Kobu wrote:

    > I've read the FAQ and several posts on multidimensional arrays and
    > how their names decay to pointer to arrays (not pointer to pointers).


    The use of the word "decay" here is common and unfortunate.
    It seems to imply that a pointer is somehow inferior to an array name
    or a "degraded" array name.
    It might be better and more correct to say that,
    "There is an implicit conversion of an array name to a pointer."

    > If this is so,
    > why does the following code fragment compiler and run correctly?:


    > cat main.c

    #include <stdio.h>

    int main(int argc, char* argv[]) {
    int example[4][4]={{ 1, 2, 3, 4},
    { 5, 6, 7, 8},
    { 9, 10, 11, 12},
    {13, 14, 15, 16}};

    printf("\nBEFORE DOUBLE DEREFERENCING of example:\n");
    printf("\nexample[0][0] = %d\n",example[0][0]);

    **example = 100; // double dereferencing 'example'

    printf("\nAFTERDOUBLE DEREFERENCING of example:\n\n\n");
    printf("\nexample[0][0] = %d\n",example[0][0]);

    return 0;
    }

    > gcc -Wall -std=c99 -pedantic -o main main.c
    > ./main


    BEFORE DOUBLE DEREFERENCING of example:

    example[0][0] = 1

    AFTERDOUBLE DEREFERENCING of example:



    example[0][0] = 100

    > The code ('example') behaves just like a pointer to a pointer.


    example is an array of four arrays of four int's each.
    *example is a reference to array 0 of example (example[0]).
    **example is a reference to element 0 of example[0] (example[0][0]).

    > Then why isn't it a pointer to a pointer?


    In general, there is no memory reserved
    for an object of type int** named example
    nor is any memory reserved for any array of four objects of type int*
    to which example points.
    example is just the name of an array of four arrays of four int's each.
     
    E. Robert Tisdale, Jan 19, 2005
    #4
  5. Kobu

    CBFalconer Guest

    Kobu wrote:
    >
    > I've read the FAQ and several posts on multidimensional arrays and how
    > their names decay to pointer to arrays (not pointer to pointers).
    >
    > If this is so, why does the following code fragment compiler and run
    > correctly?:
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > int example[4][4]={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
    > 15, 16};
    >
    > printf("\nBEFORE DOUBLE DEREFERENCING of example:\n");
    > printf("\nexample[0][0] = %d\n",example[0][0]);
    >
    > **example = 100; /* double dereferencing 'example' */
    >
    > printf("\nAFTERDOUBLE DEREFERENCING of example:\n\n\n");
    > printf("\nexample[0][0] = %d\n",example[0][0]);
    >
    > return 0;
    > }
    >
    > The code ('example') behaves just like a pointer to a pointer. Then
    > why isn't it a pointer to a pointer?


    Because the array 'example' is still in scope. Thus *example is a
    pointer to an array of 4 ints. **example points to the first of
    those 4 ints.

    However if you pass example off to another function, things are
    different.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
     
    CBFalconer, Jan 19, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Bazell

    slice of multidimensional array

    Dave Bazell, Jul 23, 2003, in forum: Perl
    Replies:
    2
    Views:
    4,120
  2. shane
    Replies:
    2
    Views:
    8,318
    shane
    Jun 4, 2004
  3. c19h28o2

    Multidimensional array pointer problem

    c19h28o2, Sep 26, 2006, in forum: C Programming
    Replies:
    6
    Views:
    489
    Barry Schwarz
    Sep 28, 2006
  4. Replies:
    18
    Views:
    765
  5. Szabolcs Borsanyi

    Cast to pointer to multidimensional array

    Szabolcs Borsanyi, Jun 6, 2008, in forum: C Programming
    Replies:
    0
    Views:
    632
    Szabolcs Borsanyi
    Jun 6, 2008
Loading...

Share This Page