An Uncommon representation of array elements
Consider the below program.
int main( ) { int arr[2] = {0,1}; printf("First Element = %d\n",arr[0]); getchar(); return 0; } |
chevron_right
filter_none
Pretty Simple program.. huh… Output will be 0.
Now if you replace arr[0] with 0[arr], the output would be same. Because compiler converts the array operation in pointers before accessing the array elements.
e.g. arr[0] would be *(arr + 0) and therefore 0[arr] would be *(0 + arr) and you know that both *(arr + 0) and *(0 + arr) are same.
Please write comments if you find anything incorrect in the above article.
Recommended Posts:
- Sum of array Elements without using loops and recursion
- How will you show memory representation of C variables?
- Difference between pointer to an array and array of pointers
- What’s difference between “array” and “&array” for “int array[5]” ?
- std::transform() in C++ STL (Perform an operation on all elements)
- Sum of an array using MPI
- How to pass an array by value in C ?
- Array class in C++
- Pointer to an Array | Array Pointer
- Pointer vs Array in C
- 4 Dimensional Array in C/C++
- Accessing array out of bounds in C/C++
- Do not use sizeof for array parameters
- Array Type Manipulation in C++
- What is Array Decay in C++? How can it be prevented?



