Why is 2[myArray] valid C syntax? [duplicate] - c

This question already has answers here:
Closed 13 years ago.
Duplicate
In C arrays why is this true? a[5] == 5[a]
Given an array
myArray[5] = { 0, 1, 2, 3, 4 };
an element can be accessed as
2[myArray]
Why? When I see this expression I'm imagining C trying to access the pointer "2" and failing to add "myArray" pointer increments to dereference that address. What am I missing?

in C, a[b] is equivalent to *(a + b). And, of course, the + operator is commutative, so a[b] is the same as b[a] is the same as *(b + a) is the same as *(a + b).

Related

What do 2[p] and 6[p] mean? [duplicate]

This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 6 years ago.
#include <stdio.h>
int main(void)
{
char c[]="GATECSIT2017";
char *p=c;
printf("%s", c+2[p]-6[p]-1);
return 0;
}
What do 2[p] and 6[p] mean?
Please provide a detailed explanation.
Output: 17
For any valid pointer or array p and index i, the expression p[i] is equal to *(p + i). And due to the commutative property of addition *(p + i) is equal to *(i + p) which is then equal to i[p].
In short, 2[p] is the same as p[2].
What do 2[p] and 6[p] mean?
2[p] is equivalent to p[2] and 6[p] is equivalent to p[6].

What does 1[index] mean in c? [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.
Why does the below code not generate an error and print the value stored in arr then followed by a junk value ?
int main() {
int arr[1]={10};
printf("%d %d\n",0[arr], 1[arr] );
return 0;
}
In C, 0[arr] == arr[0].
So,
0[arr]==arr[0]==10.
and
1[arr]==arr[1]==Junk value
Check this SO question and it's answers.
The C standard defines the [] operator as follows:
a[b] == *(a + b)
Therefore a[5] will evaluate to:
*(a + 5) and 5[a] will evaluate to:
*(5 + a) and from elementary school math we know those are equal. (Addition is commutative.)
This is the direct artifact of arrays behaving as pointers, "a" is a
memory address. "a[5]" is the value that's 5 elements further from
"a". The address of this element is "a + 5". This is equal to offset
"a" from "5" elements at the beginning of the address space (5 + a).
1[arr] is the same as arr[1].
This is possible because E1[E2] is equivalent to (*((E1) + (E2)) in C by definition of [] operator.
Note that if you declare:
int arr[1]={10};
There is only element in the array, arr[0] and there is no arr[1] element.

different representation of array element [duplicate]

This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 9 years ago.
consider following C code
int a[]={1,2,3,4};
printf("%d",2[a]);
this prints "3".How is it possible? I know in a[2] a is the base address of array.But in 2[a]
what is 2? and how it accessses array a?I am totally confused with this representation of array.
There are two things to remember here:
The first is that array access is basically just a fancy way of using pointer arithmetic. For example, if you have the array
int a[10];
then
a[3] = 5;
is equal to
*(a + 3) = 5;
The second thing to remember is that addition (like in a + 3 above) is commutative, so a + 3 is the same as 3 + a. This leads to e.g.
*(3 + a) = 5;
which can be interpreted as
3[a] = 5;
int a[]={1,2,3,4}; is an integer array containing 4 elements and a is the Base Address, Let the Base Address be denoted by X . Now a[1] means element at address X + sizeOf(int) * 2 = Y (suppose) i.e. element at address Y, likewise 2[a] means element at adsress sizeOf(int) * 2 * X = Y.
Thus even if you write a[2] or 2[a] eventually complier recognizes it as Y and refres to the element at address Y which is 3 in our case.
Hope it addresses the problem right.
*(expr1+expr2) is equivalent to expr1[expr2] or expr2[expr1].
*(expr2+expr1) is equivalent to expr2[expr1] or expr1[expr2].
It is just another way to write an element of an array.
In int a[]={1,2,3,4};, element at index 3 can be referenced by many methods:
As an array element: a[3]
Using pointer: *(a + 3) or *(3 + a) [Addition is commutative in arithmetics and in C]
Now you can write the second way of representation using pointer as 3[a], i.e. a[3] is equal to *(a + 3) is equal to *(3 + a) is equal to 3[a].

In C programming language: A[i] and i[A] both refer to the i-th element of array A. WHY? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
In C arrays why is this true? a[5] == 5[a]
While coding in C, I accidentally found that the code below correctly prints the elements of array A:
int A[] = {10, 20, 5, 32, 40};
for(int i=0; i<5; i++)
printf("%d \n", i[A]);
So i[A] acts likeA[i].
Why? what is the logic behind this behavior?
Because the subscript operator in C is defined in terms of pointer arithmetic, see
(C99, 6.5.2.1p2) "The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2)))."
it's commutativity of addition:
*(A+i) same as *(i+A)

Undefined behavior while using increment operator [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 3 years ago.
I am new to C, i have an Increment operator program in C
#include<stdio.h>
main(){
int a, b;
a = 2;
b = a + ++a + ++a;
printf("%d", b);
getchar();
}
The output is 10, can someone explain me how the output will be 10 .
a + ++a + ++a;
Behaviour for this is undefined.
The compiler might generated code that evaluated this as 2 + 4 + 4 or 3 + 3 + 4, but any combination/ordering of incrementing and accessing is a "valid" result.
This is undefined, the ++i can happen in any order.
Function call arguments are also ambigiously evaluated, e.g. foo(++i,++i).
Not all operator chains are undefined, a||b||c is guaranteed to be left-to-right, for example.
The guarantees are made in places known as sequence points although this terminology is being deprecated and clarified in C++0x.
What's odd in your example is that neigher 2+3+4 nor 4+4+3 happened, so the compiler evaluated the left side first in one step and the right side first in the other. This was probably an optimisation to flatten the depencency graph.

Resources