Need help to understand x["AC"] in the following code

This is a discussion on Need help to understand x["AC"] in the following code within the C Programming forums, part of the General Programming Boards category; Hello I just start to learn C. Is someone could help me to understand the x["AC"] on the line putchar( ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Location
    Québec, Canada
    Posts
    2

    Need help to understand x["AC"] in the following code

    Hello

    I just start to learn C. Is someone could help me to understand the x["AC"] on the line putchar( x["AC"] ); This is the only one line I do not understand in this code. Thanks a lot for help.

    Code:
    #include <stdio.h>
    
    int main() 
    {
        int x;
           
        for ( x = 0; x < 2; x++) {
          putchar( "AC"[x] );
          putchar( x["AC"] );
          putchar( *("AC" + x) );
          putchar( *"AC" + x );
    
          putchar('\n');
        } 
        return 0;
    }

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    [EDIT]

    Oops, I re-read your post properly this time...

    And I've never seen arrays used like that, so I'll have to leave it to someone else to explain.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    I am very surprised if this compiles because "AC" is not exactly an internal constant. That said, all four statements do exactly the same thing: dereference a particular cell of memory.
    Code:
    int x[] = { 0, 1, 2, 3 };
    printf("%d ", x[0]);
    printf("%d ", 1[x]);
    printf("%d ", *(x + 2));
    C is very flexible in that x[3] dereferences the same as 3[x], and same as *(x + 3)

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    "AC"[x] is the same as *("AC" + x) which is the same as *(x + "AC"), which is the same as x["AC"] because addition is commutative.
    My best code is written with the delete key.

  5. #5
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    >C is very flexible in that x[3] dereferences the same as 3[x]

    So, you can switch the index with the name and it is valid?

    I don't like it... :P
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  6. #6
    Registered User
    Join Date
    Jun 2006
    Location
    Québec, Canada
    Posts
    2
    Thanks a lot citizen. I did not know about the 3[x] notation.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    > I don't like it... :P
    People who obfuscate code know this about you.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >So, you can switch the index with the name and it is valid?
    Yes and no. As long as the resulting syntax is valid and the behavior is identical, it works. But take a[x * y]. For the switch to work, you need to surround the expression in parens: (x * y)[a].
    My best code is written with the delete key.

  9. #9
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Quote Originally Posted by Prelude
    Yes and no. As long as the resulting syntax is valid and the behavior is identical, it works. But take a[x * y]. For the switch to work, you need to surround the expression in parens: (x * y)[a].

    Amazing, I had no idea. Is there any practical use for this?
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is there any practical use for this?
    No, it's just some minor trivia that fell out of how arrays are parsed.
    My best code is written with the delete key.

  11. #11
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Fair enough. :P
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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