Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 574 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
With arrays, why is it the case that a[5] == 5[a]?

#1
As Joel points out in [Stack Overflow podcast #34](

[To see links please register here]

), in [C Programming Language](

[To see links please register here]

) (aka: K & R), there is mention of this property of arrays in C: `a[5] == 5[a]`

Joel says that it's because of pointer arithmetic but I still don't understand. **Why does `a[5] == 5[a]`**?
Reply

#2
One thing no-one seems to have mentioned about Dinah's problem with `sizeof`:

You can only add an integer to a pointer, you can't add two pointers together. That way when adding a pointer to an integer, or an integer to a pointer, the compiler always knows which bit has a size that needs to be taken into account.
Reply

#3
Because array access is defined in terms of pointers. `a[i]` is defined to mean `*(a + i)`, which is commutative.
Reply

#4
To answer the question literally. It is not always true that `x == x`

double zero = 0.0;
double a[] = { 0,0,0,0,0, zero/zero}; // NaN
cout << (a[5] == 5[a] ? "true" : "false") << endl;

prints

false
Reply

#5
For pointers in C, we have

a[5] == *(a + 5)

and also

5[a] == *(5 + a)

Hence it is true that `a[5] == 5[a].`
Reply

#6
in c compiler

a[i]
i[a]
*(a+i)

are different ways to refer to an element in an array ! (NOT AT ALL WEIRD)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through