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.
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 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 ;))
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 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 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]);