What if array name is put in square brackets? [duplicate] - arrays

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.

Related

Pointer inside a square bracket? [duplicate]

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

How does this way of accessing character in C-String work? [duplicate]

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.

difference between a[10] and 10[a] [duplicate]

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.

Array output miscellaneous [duplicate]

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.

Is chars[4] and 4[chars] the same in C? Why? [duplicate]

This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 7 years ago.
I've read this and don't believe it :) I've no compiler here to test.
In raw C, the [] notation is just a pointer math helper. Before [], you'd look for the fourth char in the block pointed to by ptr like:
*(ptr+4)
Then, they introduced a shortcut which looked better:
ptr[4]
Which transaltes to the earlier expression. But, if you'd write it like:
4[ptr]
This would translate to:
*(4+ptr)
Which is indeed the same thing.
Because a[b] is exactly the same as *(a+b), and + is commutatitve.
chars[4] is *(chars+4), and 4[chars] is *(4+chars)
http://c-faq.com/aryptr/joke.html
Try this to test compile: http://codepad.org/

Resources