This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 3 years ago.
Consider the following code:
#include <stdio.h>
int main(void){
char *p="A for Apple";
char q[15]="B for Ball";
printf("x=%c y=%c\n",*(p+10)-2,*(&q[4]-2));
printf("u=%c v=%c w=%d\n",p[8],*q+8,8[p]-q[8]);
return 0;
}
Output:
x=c y=f
u=p v=J
w=4
Problem, I am having here is determining how w=4 was evaluated.
What does 8[p] means?
a[x] is shorthand for *(a + x). Consequently, a[x] is equivalent to the expression x[a]. (same holds for 8[p] and p[8] of course ;))
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:
What does 1[d=b] mean?
(3 answers)
Closed 5 years ago.
I came across a code as in below
#include <stdlib.h>
int main(){
char a[]="0123456789";
printf("%s\n",a+6[a]-2[a]);
return 0;
}
Output
456789
How does the calculation of a+6[a]-2[a] happens in printf?
Why giving just 6[a] in printf doesn't work?
printf("%s\n",6[a]);
Well, a statement like
a+6[a]-2[a]
can be re-written as
&(a[ a[6] - a[2] ])
which is simply,
use the value of a[6] (type, int) as the index in the first case
use the value of a[2] (type int) as the RHS.
The result, is a pointer, is passed to printf() as an argument to %s conversion specification.
This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 7 years ago.
#include <stdio.h>
int main(int argc, char *argv[]){
int *ia[5]={0,1,2,3,4};
iap=ia;
printf("ia[3]:%d\n",3[ia]);
return 0;
}
Why is that line working in C?
The amazing world of C pointer arithmetic:
ia[3] is computed as *(ia + 3), which is the same as *(3 + ia) or 3[ia].
You can also write 1[ia+2], or 3[ia-+!ia], or even 2[1+ia, ia+1]...
None of these should appear in regular code unless you are trying to obfuscate and confuse the casual reader/maintainer/code reviewer.
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 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.