#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3

    Pointer levels of indirection.


    I'm sorry for spamming about pointers, but i'm close to understanding it fully. :rolleyes:

    In the following code:
    Code:
    char a[3]= {'1','2','3'};
    
    char *p[3]={a, a+1, a+2};
    
    char **m=p;
    
    printf("%c    %c", **p, **m ); BOTH VALUES ARE EQUAL !
    
    OUTPUT: 1       1
    Why do i have to use ** in both cases, instead of: * in the first and: ** in the second ?? Aren't these different levels of indirection ?

    p is a pointer to char, and m is a pointer to pointer to char. So basically, a is level 0, p is level 1 and m is level 2 !
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    Being an array adds another level of indirection:
    Originally Posted by Christopher Skelly
    {ellipses implied}
    Key Fact #7 The name of an array usually behaves as if the array name were a pointer value.

    Key Fact #8 The name of an array, in almost every context, evaluates to the address of the array's own "zeroth" element.

    The [] operator, usually thought of as being related to arrays, is also a dereferencing operator. p[n] lives on the plane below p. To see that this is the case consider the simple array declaration

    int arr[10];
    What level does the expression arr have in most contexts? arr is a level-one expression, evaluating to the address of the zeroth element of arr, in virtually every context.

    In any event, arr will typically behave like a level-one value. What about arr[0]? arr[0] is clearly a level 0 value. arr[0] represents the actual data in the first element of the arr array. So the subscript brings you down one level of indirection, just as the * did in a dereference.

    Key Fact #10 The six moves of pointer dominos are:

    3. Go down one level of indirection using []


    The [] operator is also a dereferencing operator. a[n] lives on the plane below a, where a is any address expression. An important principle of pointer dominos is the fact that both * and [] bring an expression down one level of indirection from what they are applied to. The formula which relates how * and [] are related is the important formula:

    a[n] == *(a + n)

    This master formula in C and C++ shows that a subscript is syntactically equivalent to dereferencing an offset from a pointer. The a in the formula represents any address. The address may come from a pointer, or from an array name, or from a casted expression. It doesn't matter. The subscript can always be applied to an address, just like the *, and the relation between the two forms comes from this formula.
    Originally Posted by C_Learner
    p is a pointer to char, and m is a pointer to pointer to char. So basically, a is level 0, p is level 1 and m is level 2 !
    No, a is Level 1, being an array. That makes a a char*. p is an array of char*, making it a Level 2, a char**. m is a char** as per its declaration. p and m are both at the same level of indirection.

    BTW,try to combine different levels of indirection (eg, assignments, comparisons, not dereferencing all the way down to the struct) should cause the compiler to throw warnings at you.

    {Note to lurkers: quotes of Christopher Skelly are from Pointer Power in C and C++, Parts 1 and 2, The C Users Journal, Feb and Mar 1993, reposted at http://collaboration.cmc.ec.gc.ca/sc...lly/skelly.htm (Part 1) and at http://collaboration.cmc.ec.gc.ca/sc...lly/skelly.htm (Part 2).}
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3
    Originally Posted by dwise1_aol
    Being an array adds another level of indirection:



    No, a is Level 1, being an array. That makes a a char*. p is an array of char*, making it a Level 2, a char**. m is a char** as per its declaration. p and m are both at the same level of indirection.

    BTW,try to combine different levels of indirection (eg, assignments, comparisons, not dereferencing all the way down to the struct) should cause the compiler to throw warnings at you.

    {Note to lurkers: quotes of Christopher Skelly are from Pointer Power in C and C++, Parts 1 and 2, The C Users Journal, Feb and Mar 1993, reposted at http://collaboration.cmc.ec.gc.ca/sc...lly/skelly.htm (Part 1) and at http://collaboration.cmc.ec.gc.ca/sc...lly/skelly.htm (Part 2).}
    >>Will it be true if i say, that the elements of an array ABOVE, are on the same level as the NAME of the array BENEATH it?? (considering that both are arrays of pointers)?

    Because, based on these rules, wouldn't it make app[] in Figure1(http://collaboration.cmc.ec.gc.ca/sc...kelly/fig1.htm) a level 4? Because, for examle they say that ap[] is above it's elements, which are all pointers. Isn't app[] the same ? above its elements which are all pointers ? (to them app is level 3 just like its elements :confused: ) Or is my first question the answer to the second ? (=that the elements of app[i] are level 2 just like ap itself?).

IMN logo majestic logo threadwatch logo seochat tools logo