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].
Related
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.
This question already has answers here:
Pointer Arithmetic In C
(2 answers)
Closed 8 years ago.
I'm not getting the output. Why it is happening?
#include <stdio.h>
int main(void){
int a[3][3];
int *p, *q;
p=a[0];
q=a[1];
printf("%d\n",sizeof(int));
printf("%d\n",q-p);
printf("%d %d\n",q,p);
return 0;
}
Output
4
3
2686728 2686716
I thought (q-p) should be 12! Is my math degrading?!
I thought (q-p) should be 12
No. (q-p)==3 shall hold true since they have type int*. Meanwhile it's true that q == p + 3.
Also this is true: (char*)q - (char*)p == 12
You are getting output value as 3, because both pointers are pointing to integer data type and the difference between them is three objects.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In C arrays why is this true? a[5] == 5[a]
How can X[i] possibly be interpretted the same as i[X] in C?
I think the question is asking how an array[elementInArray] can be interpretted the same as elementInArray[array]
Also, unrelated on the same homework:
"Explain why the "hidden bit" of floating point format does not need to be represented." (in terms of 32 bit words of binary)
In C, X[i] = i[X] = *(X + i) = *(i + X). As per commutative property, they are same.
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)
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).