Why is a[2] == 2[a] ??

This is a discussion on Why is a[2] == 2[a] ?? within the C Programming forums, part of the General Programming Boards category; Why is a[2] == 2[a] the same?? I dont get a sintax error...could someone explain?...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    3

    Why is a[2] == 2[a] ??

    Why is a[2] == 2[a] the same?? I dont get a sintax error...could someone explain?

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    that is because a[2] is evaluated as *(a+2) where a is the starting address of the array elements..similarly, when you say 2[a], it is evaluated as *(2+a) which gives the same result.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    3
    GOT IT! thx dude!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Mmm, one of those weird and interesting tricks which serves no useful purpose in the real world.
    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.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    3
    I know!! But it comes on exams!

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Do your own homework. Someone black flag this guy.
    If you understand what you're doing, you're not learning anything.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    > I know!! But it comes on exams!
    Which usually tells me more about the examiner than the examiner would rather not have known about them...
    Like whether you're being taught "how to program in C", or "A bunch of dumb tricks using void main, gets() and fflush(stdin)"
    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. Homework help
    By mkdl750 in forum C Programming
    Replies: 45
    Last Post: 07-17-2008, 10:44 PM
  2. Separate long string into multiple arrays
    By cashmerelc in forum C Programming
    Replies: 6
    Last Post: 11-27-2007, 02:57 AM
  3. Tic Tac Toe program...
    By Kross7 in forum C++ Programming
    Replies: 12
    Last Post: 04-12-2007, 04:25 PM
  4. Need an extra pair of eyes ....
    By Whoputthatthere in forum C Programming
    Replies: 5
    Last Post: 06-02-2006, 01:19 PM
  5. Need help with Tic Tac Toe
    By Zerostatic in forum C++ Programming
    Replies: 19
    Last Post: 07-19-2002, 08:20 PM

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