Thread: Pointer expr *--*++p+3 ???

  1. #1
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Pointer expr *--*++p+3 ???

    *--*++p+3 ==> i dnt know how this expr is evaluated... I m getting ck as output..


    Code:
        static char *s[]={"black","white","yellow","violet"};
        char **ptr[]={s+3,s+2,s+1,s},***p,*gt;
        p=ptr;
        **++p; // p points to "Yellow"
        printf("%s",*--*++p+3);         // *--*++p+3 this one i cant understand
    It is taken from a C Quiz.....

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Yikes. Talk about a nightmare... I will go ahead and leave this for someone better at it than me...

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The things to remember are:
    1) All the prefix stuff happens first (in this example).
    2) Pointer arithmetic moves by the size of the thing pointed to.

    So for instance, p is a char***, currently pointing to the second char** in ptr. Then ++ will move forward by one char**, meaning p then points to the third char** in ptr. The * then dereferences, so *++p is a char**, specifically it is the third char** in ptr, namely s+1. The -- then moves back by one char* (since a char** points to a char*), and et cetera.

  4. #4
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Cool

    Thanks tabstop for ur Simple and nice Explanation .. I understood

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    I don't know if this helps but the implied parenthesization is
    Code:
    (*(--(*++p)))+3
    and read it from the inside out ie
    (*++p) increments p by one and and dereferences to give s+1
    --(s+1) decrements the expression by one to give s
    *(s) dereferences s to give s[0]
    s[0] points to the 1st char of string "black"; and
    s[0]+3 points 3 characters past "b"

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    s[0]+3 would cast s[0] into an integer and add 3 to it, wouldn't it?

    s[0][3] would give 3 characters past 'b' in black ('c').

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Bladactania View Post
    s[0]+3 would cast s[0] into an integer and add 3 to it, wouldn't it?

    s[0][3] would give 3 characters past 'b' in black ('c').
    s[0] is char* why whould it be casted to int?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    because 3 is an int. When doing mathematical expressions each part is converted to the highest order type, in this case an int.

    like if you do 4.0 + 4 you get a float result.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Bladactania View Post
    s[0]+3 would cast s[0] into an integer and add 3 to it, wouldn't it?
    Nope!
    Quote Originally Posted by Bladactania View Post
    s[0][3] would give 3 characters past 'b' in black ('c').
    An array and index expression is the same when written as a pointer and an offset so s[0][3] == s[0]+3

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Bladactania View Post
    because 3 is an int. When doing mathematical expressions each part is converted to the highest order type, in this case an int.

    like if you do 4.0 + 4 you get a float result.
    you need to learn a little about pointer arithmetics, pointer + integer is still a pointer of the same type
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    so what if s was an array of integers and I wanted to add 3 to it?

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    and s[0] == "black". so "black" + 3 = what?

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Bladactania View Post
    so what is s was an array of integers and I wanted to add 3 to it?
    you will get pointer to the third element of course
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    ok, but if s[0] = 5, I want the result of my expression to equal 8, how would I code it?

  15. #15
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Bladactania View Post
    because 3 is an int. When doing mathematical expressions each part is converted to the highest order type, in this case an int.

    like if you do 4.0 + 4 you get a float result.
    AFAIK pointer arithmetic isn't treated that way; for ex. adding a float to a pointer doesn't make sense so it generates a compile time error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM