pointer madness

This is a discussion on pointer madness within the C Programming forums, part of the General Programming Boards category; from what i've heard, c transforms expr1[expr2] in *(expr1+expr2), right? but this is madness it means that you could, for ...

  1. #1
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46

    pointer madness

    from what i've heard, c transforms expr1[expr2] in *(expr1+expr2), right?

    but this is madness it means that you could, for example, say:
    i++[x]; // where x is a array (pointer)

    seems crazy to me. Is it correct?

    2nd thought: if it IS correct, then how will you obsfucate multi dimenssional arrays? j[i++[x[k]][l] ??? how does the compiler know which is the column or the line?

    this is really madness now...
    Two strings walk into a bar. The first one says, 'Bartender! Bartender! I want a drink!'. The second one says, 'Bartender! Bartender! I want a drink too! Blaaaaaaaaah eeeeeeeek yaaaaaaak oooooooh'. The first one says, 'Please excuse my friend. He isn't null term--'.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's a good reference.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    well, i found out that i++[x] is correct, but i'm still not clear about obfuscated examples. does any compiler has its rules?
    Two strings walk into a bar. The first one says, 'Bartender! Bartender! I want a drink!'. The second one says, 'Bartender! Bartender! I want a drink too! Blaaaaaaaaah eeeeeeeek yaaaaaaak oooooooh'. The first one says, 'Please excuse my friend. He isn't null term--'.

  4. #4
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    also about obfuscated things, look at the following example:

    *++b ? (*++b + *(b-1)) : 0

    b-1 is calculated with b or with ++b ?

    i'm just asking because i've come across some 'nice' code...
    Two strings walk into a bar. The first one says, 'Bartender! Bartender! I want a drink!'. The second one says, 'Bartender! Bartender! I want a drink too! Blaaaaaaaaah eeeeeeeek yaaaaaaak oooooooh'. The first one says, 'Please excuse my friend. He isn't null term--'.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Why are you even bothering with this stuff?

    1. Changing a[i] into i[a] has NO practical applications, so whilst it's a curiosity, it's not useful enough to worry about.

    2. *++b ? (*++b + *(b-1)) : 0
    Read the FAQ which Quzah posted (all of it).
    Find out why all expressions with multiple side effects are undefined, no matter how 'nice' they are.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    well, just asking... i'm not using them don't worry.
    Two strings walk into a bar. The first one says, 'Bartender! Bartender! I want a drink!'. The second one says, 'Bartender! Bartender! I want a drink too! Blaaaaaaaaah eeeeeeeek yaaaaaaak oooooooh'. The first one says, 'Please excuse my friend. He isn't null term--'.

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by Salem
    2. *++b ? (*++b + *(b-1)) : 0
    Read the FAQ which Quzah posted (all of it).
    Find out why all expressions with multiple side effects are undefined, no matter how 'nice' they are.
    Isn't this statement okay?
    Code:
     *++b ? (*++b + *(b-1)) : 0
    Isn't b only modified once in between sequence points?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by pianorain
    Isn't this statement okay?
    Code:
     *++b ? (*++b + *(b-1)) : 0
    Isn't b only modified once in between sequence points?
    Which happens first, *++b or *(b-1)?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by Dave_Sinkula
    Which happens first, *++b or *(b-1)?
    http://www.cppreference.com/operator_precedence.html
    Seems simple enough. (b-1) would be evaluated first.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Seems simple enough. (b-1) would be evaluated first.
    Or perhaps second. There's no guarantee.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I believe the order of evaluation for binary + is unspecified, so either one could happen before the other.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    After playing with it and reading back through previous threads, I see that I was totally ignoring the following sentence: "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored." Makes sense now.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 07:23 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21