Confusion with C

Discussion in 'C Programming' started by hari4063, Oct 30, 2004.

  1. hari4063

    hari4063 Guest

    Every day I prove that i am beginer with C (even I program in C or/and C++
    for more than 5 years)! Some days ago I find code that use _REALY_ strange
    sintax, something like this:

    // ------- code ------
    int a[10];
    int i;
    for(i=0; i<10; i++)
    i[a] = i;
    // --- end ------------

    At first I think that is some compiler specific _hack_, but i find
    something that is called "array comutation"? So, using a is same like
    *((a)+(i)). With this, upper code have (just little bit) logic.

    My questiona are:
    1) is this portable
    2) is this truely what C creators wanna to have, or just parser use this
    becuase *(a+i) rule?

    Thanks.
     
    hari4063, Oct 30, 2004
    #1
    1. Advertising

  2. hari4063 <> wrote:
    > My questiona are:
    > 1) is this portable
    > 2) is this truely what C creators wanna to have, or just parser use this
    > becuase *(a+i) rule?


    ISO/IEC 9899:1999 (E), 6.5.2.1 Array subscripting

    2 A postfix expression followed by an expression in square brackets [] is a
    subscripted designation of an element of an array object. The definition
    of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))).
    Because of the conversion rules that apply to the binary + operator, if E1
    is an array object (equivalently, a pointer to the initial element of an
    array object) and E2 is an integer, E1[E2] designates the E2-th element of
    E1 (counting from zero).


    Flo
    --
    Florian Weingarten | IRCnet: fw (fw_) | PGP: 0x65C91285
    http://hackvalue.de/~flo/ | ICQ: 38564900 | Jabber:
     
    Florian Weingarten, Oct 30, 2004
    #2
    1. Advertising

  3. hari4063 wrote:

    > Every day, I prove that I am beginner with C
    > (even I program in C or/and C++ for more than 5 years)!
    > Some days ago I found code that uses _REALLY_ strange syntax,
    > something like this:


    > // ------- code ------
    > int a[10];
    > int i;
    > for(i=0; i<10; i++)
    > i[a] = i;
    > // --- end ------------


    This is bad code. You should write:

    int a[10];
    for (int i = 0; i < 10; ++i)
    a = i;
    >
    > At first I think that is some compiler specific _hack_,
    > but I found something that is called "array commutation"?
    > So, using a is same like *((a)+(i)).
    > With this, upper code have (just little bit) logic.


    > My questions are,
    > 1) "Is this portable?",
    > 2) "Is this truely what C creators wanna to have?"
    > "Or just parser use this because *(a+i) rule?"


    It is portable.
    The language designers meant to have this flexibility.
    You code doesn't show any motivation for "array commutation".
    Why didn't you post the code that you found?
     
    E. Robert Tisdale, Oct 30, 2004
    #3
  4. hari4063

    Mike Wahler Guest

    "E. Robert Tisdale" <> wrote in message
    news:clum05$4lg$...
    > hari4063 wrote:
    >
    > > Every day, I prove that I am beginner with C
    > > (even I program in C or/and C++ for more than 5 years)!
    > > Some days ago I found code that uses _REALLY_ strange syntax,
    > > something like this:

    >
    > > // ------- code ------
    > > int a[10];
    > > int i;
    > > for(i=0; i<10; i++)
    > > i[a] = i;
    > > // --- end ------------

    >
    > This is bad code.


    It's perfectly valid.

    > You should write:
    >
    > int a[10];
    > for (int i = 0; i < 10; ++i)
    > a = i;


    This will result in the exact same behavior as that
    of the code above.

    -Mike
     
    Mike Wahler, Oct 30, 2004
    #4
  5. hari4063

    Mike Wahler Guest

    "hari4063" <> wrote in message
    news:...
    > Every day I prove that i am beginer with C (even I program in C or/and C++
    > for more than 5 years)! Some days ago I find code that use _REALY_ strange
    > sintax, something like this:
    >
    > // ------- code ------
    > int a[10];
    > int i;
    > for(i=0; i<10; i++)
    > i[a] = i;
    > // --- end ------------
    >
    > At first I think that is some compiler specific _hack_, but i find
    > something that is called "array comutation"? So, using a is same like
    > *((a)+(i)).


    Yes. Also the same as i[a]

    > With this, upper code have (just little bit) logic.
    >
    > My questiona are:
    > 1) is this portable


    Yes.

    > 2) is this truely what C creators wanna to have, or just parser use this
    > becuase *(a+i) rule?


    http://www.eskimo.com/~scs/C-faq/s6.html
    http://www.eskimo.com/~scs/C-faq/q6.11.html

    -Mike
     
    Mike Wahler, Oct 30, 2004
    #5
  6. On Fri, 29 Oct 2004 17:09:02 -0700, "E. Robert Tisdale"
    <> wrote:

    >hari4063 wrote:
    >
    >> Every day, I prove that I am beginner with C
    >> (even I program in C or/and C++ for more than 5 years)!
    >> Some days ago I found code that uses _REALLY_ strange syntax,
    >> something like this:

    >
    >> // ------- code ------
    >> int a[10];
    >> int i;
    >> for(i=0; i<10; i++)
    >> i[a] = i;
    >> // --- end ------------

    >
    >This is bad code. You should write:
    > int a[10];
    > for (int i = 0; i < 10; ++i)
    > a = i;


    In the older C compilers, variables may only be initialized at the top of a
    block. Placing it within the for-loop (or after a regular statement)
    causes a compilation error.

    Also, changing the 'i++' around is absolutely unnecessairy in C:
    http://www.eskimo.com/~scs/C-faq/q3.12.html
    (Mabye in C++, but not C.)
     
    Raymond Martineau, Oct 30, 2004
    #6
  7. hari4063

    hari4063 Guest

    Re: Confusion with C Part II

    Thanks to all. I find this code in obfuscated context :) Another question :
    is there any problem/algo/whatever that use this sintax and that can not be
    written with (let say) normal array subscription?
     
    hari4063, Oct 31, 2004
    #7
  8. Re: Confusion with C Part II

    In article <>,
    hari4063 <> wrote:
    >Thanks to all. I find this code in obfuscated context :) Another question :
    >is there any problem/algo/whatever that use this sintax and that can not be
    >written with (let say) normal array subscription?


    No, because any use of i[a] can trivially be rewritten as a.

    A more interesting question is whether there's anywhere where this might
    be useful, f'rexample by making code clearer.

    I asked about that a while ago, and one reply mentioned that if you're
    storing related values as a set of arrays rather than as an array of
    structs, reversing the normal indexing might make that a bit clearer:
    --------
    for(i=0;i<nelems;i++)
    {
    /*Doing things this way emphasizes the "field names" that we're
    interested in
    */
    i[output]=mangle(i[input1],i[input2]);
    }
    --------

    Unless there's a good reason not to use an array of structs, though,
    this can be improved even more by doing so:
    --------
    for(i=0;i<nelems;i++)
    {
    arr.output=mangle(arr.input1,arr.input2);
    }
    --------


    dave

    --
    Dave Vandervies
    [They think] that spinning your tires madly makes you accelerate faster. When
    they catch up to my minivan at the next light, I try to explain this fallacy of
    their thinking to them as well. --Paul Tomko in the scary devil monastery
     
    Dave Vandervies, Oct 31, 2004
    #8
  9. Re: Confusion with C Part II

    On Sat, 30 Oct 2004 19:08:06 -0400, "hari4063"
    <> wrote:


    >Thanks to all. I find this code in obfuscated context :) Another question :
    >is there any problem/algo/whatever that use this sintax and that can not be
    >written with (let say) normal array subscription?


    There is nothing that cannot be done only by one method because the
    methods aren't just similar, they're functionally and syntactically
    identical. Other than for obfuscated code, I have found only one use for
    the 'inverted' subscript: portable and obvious conversion from an index to
    a hex digit.

    "0123456789ABCDEF"[ i ]

    where it is known to be in the range of 0 through 15. And even this use
    is merely cute; it's not easier to read (or harder). It does avoid the
    problem of

    HexChars[ i ]

    where HexChars is a string defined elsewhere that might have been changed
    - but that's not really a legitimate excuse.


    --
    #include <standard.disclaimer>
    _
    Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
    Per the FCA, this address may not be added to any commercial mail list
     
    Kevin D. Quitt, Oct 31, 2004
    #9
  10. Re: Confusion with C Part II

    In article <>, Kevin D. Quitt <> writes:
    >
    > Other than for obfuscated code, I have found only one use for
    > the 'inverted' subscript: portable and obvious conversion from an index to
    > a hex digit.
    >
    > "0123456789ABCDEF"[ i ]
    >
    > where it is known to be in the range of 0 through 15.


    But that's a normal use of array notation, with the subscript in the
    square brackets. The "inverted" usage would be:

    i["0123456789ABCDEF"]

    If "i" is replaced with any sort of more complex expression, though,
    putting it in the square brackets is clearly better, since it saves
    having to introduce parentheses. For example, if we don't want to
    assume i is in the domain:

    "0123456789ABCDEF"[i & 0xf]

    versus

    (i & 0xf)["0123456789ABCDEF"]

    Of course, none of these are likely to come up all that often. (I
    have boilerplate "hex dump" code I use in various projects, and I
    bet many others here do, but aside from that how often does the
    literal string array syntax trick appear?)

    --
    Michael Wojcik

    The lark is exclusively a Soviet bird. The lark does not like the
    other countries, and lets its harmonious song be heard only over the
    fields made fertile by the collective labor of the citizens of the
    happy land of the Soviets. -- D. Bleiman
     
    Michael Wojcik, Nov 1, 2004
    #10
  11. Re: Confusion with C Part II

    On 1 Nov 2004 17:14:19 GMT, (Michael Wojcik) wrote:


    >
    >In article <>, Kevin D. Quitt <> writes:
    >> "0123456789ABCDEF"[ i ]

    >But that's a normal use of array notation, with the subscript in the
    >square brackets.


    Of course. I knew that, but I had a really good reason for putting it
    that way. Honest. Would a magician lie to you? Well, yes, but that's
    another story...


    --
    #include <standard.disclaimer>
    _
    Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
    Per the FCA, this address may not be added to any commercial mail list
     
    Kevin D. Quitt, Nov 4, 2004
    #11
    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. martin f. krafft

    confusion when resetting registers

    martin f. krafft, Aug 18, 2004, in forum: VHDL
    Replies:
    2
    Views:
    475
    Paul Sereno
    Aug 19, 2004
  2. Peter Hermansson

    Procedures in testbench confusion

    Peter Hermansson, Aug 25, 2004, in forum: VHDL
    Replies:
    2
    Views:
    879
    Peter Hermansson
    Aug 25, 2004
  3. Chris

    defined() confusion

    Chris, Dec 11, 2003, in forum: Perl
    Replies:
    2
    Views:
    516
  4. Jerome

    runtime confusion

    Jerome, Dec 3, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    410
    Jerome
    Dec 3, 2005
  5. Chris Jackson

    MissingMethodException Confusion

    Chris Jackson, Jul 11, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    1,831
    Luke Zhang [MSFT]
    Jul 17, 2003
Loading...

Share This Page