This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 10 years ago.
following code
a[10] == 10[a]
the result seems true in C-language
how C compiler sees both of them as the same ?
The compiler sees as follows:
a[10] == *(a + 10) == *(10 + a) == 10[a]
Check this for a better explanation.
a[10] means: "Start at memory address 10, add a to it and reference the resulting location"
10[a] means: "Start at memory address a, add 10 to it and reference the resulting location"
Since a + 10 is the same as 10 + a, both expressions will refer to the same memory location.
Related
This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 1 year ago.
I saw such a piece of C code:
int main()
{
static int a[] = {7,8,9};
printf("%d", 2[a] + a[2]);;
return 0;
}
What does 2[a] mean here?
a[b] and b[a] are 100% equivalent in C. What you have there is a very unidiomatic way of writing a[2].
By way of more complete explanation, array subscript notation a[b] is also 100% equivalent to *(a + b), which may make the reason it works both ways clearer.
This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 5 years ago.
This question was asked in a recent exam where the candidate had to find the output for the following code:
#include<stdio.h>
#include<string.h>
int main()
{
char * c = "GATECSIT2017";
char *p = c;
printf("%d", (int)strlen(c+2[p]-6[p]-1));
return 0;
}
I started coding in C++ , so I wasn't an expert in C-Strings. After messing around with code, I understood how it worked, but one thing wasn't clear to me, which was this way of accessing a char from the char* string.
My question is, how 2[p] gets the character at index 2?
Does it resolve into *(p+2) or is there something else going on? Also, is there any documentation or any article that could explain this behavior?
For any array or pointer a and index i, the expression a[i] is equal to *(a + i).
Because of the commutative property of + the expression *(a + i) is equal to *(i + a), which according to the first equality is equal to i[a], i.e. switching place of the index and pointer/array.
So in your case the expression 2[p] is equal to *(2 + p) which is equal to *(p + 2) which is equal to p[2].
Using e.g. 2[p] is only for (bad) obfuscation so please don't use it anywhere.
This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 8 years ago.
Can anyone help me out, I am not getting how gcc is compiling the below statement and printing its output.:-
printf("%d",7["sunderban"]);
C allows to access array's elements in two ways (see Accessing arrays by index[array] in C and C++ and the answers) :
int v[5];
// 1)
v[2] = 33;
// 2)
2[v] = 44;
So, what happens in your case is that you access the 8th element of the string, and it is interpret as int by the printf.
This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 9 years ago.
I am using Ubuntu 12.04lts with the GCC compiler. This program gives the result 10. Could you anybody please describe why this program gives the result like this?
#include <stdio.h>
void main(void)
{
int arr[1] = {10};
printf("\n%d\n\n", 0[arr]);
}
arr[0] gets internally expanded to *(arr+0). Similarly 0[arr] gets expanded to *(0+arr) which points to the same thing. Hence you see 10.
In general for an array or a pointer a, a[b] always means *(a+b) where a is the starting address of the array or pointer and b is the offset. Thus, a[b] and b[a] are equivalent.
below line means arr is int type array and it has size 1 and it is initialized with 10 ie index 0 has 10
int arr[1] = {10};
then next line printf statement printing the value of arr at index 0.
printf("\n%d\n\n",0[arr]);
This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 8 years ago.
#include <stdio.h>
int main()
{
int a=3, b = 6;
printf(&a["Hi!Hello! %s\n"], &b["Mnnit/Softathalon"]);
printf(&a["WHAT%c%c%c %c%c %c !\n"], 1["this"],
2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
return 0;
}
output:
Hello! Softathalon
That is C !
Why is this the output? Can anyone explain different format specifier in it?
For any array T arr[N], the expression arr[i] is equivalent to *(arr + i).
Because the addition is commutative in the latter expression, you can also write this as *(i + arr), and hence as i[arr].
In particular, arr[3] and 3[arr] denote the same thing.
It's one of those "curiously funny things you can do in C", but it should go without saying that serious code should never actually use such a construction.