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.
Related
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].
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So, I got this for my computer exam (C gcc-4.9.2):
Will the following statements give the same output? Assume a is an array.
a) printf("%d", a + 1);
b) printf("%d", &a + 1);
c) printf("%d", a[0] + 1);
Will the following statements give the same output?
Short answer: No.
Details below:
Assuming a to be defined as
int a[N];
with
N >= 1
and at least the 1st element of a had been initialised properly (case 3. depends on this latter perquisite).
printf("%d", a + 1);
a + 1
evaluates to a pointer with the value
((char*) &a[0]) + 1 * sizeof a[0]
Printing it using %d invokes UB. Anything could happen/be printed. Use %p to print void-pointer's value. Cast the pointer passed using (void*).
printf("%d", &a + 1);
&a + 1
evaluates to a pointer with the value
((char*) &a) + 1 * sizeof a
(The latter value is different from the result of 1. if N > 1.)
Printing it using %d invokes UB. Anything could happen/be printed. Use %p to print void-pointer's value. Cast the pointer passed using (void*).
printf("%d", a[0] + 1);
Some integer value + 1 as per the array's 1st element's initialisation will be printed.
a) printf("%d", a + 1); takes the address of the array a. Adding 1 to it means the pointer now points to the second element (i.e. the pointer is incremented with sizeof(int) bytes). Printing an address as a number is not defined. To print an address, use printf("%p", a);.
b) printf("%d", &a + 1); In &a the type is 'pointer to array of int' and adding 1 will add the size of the whole array, not of an int. So the address of the first element beyond the array will be provided to printf (see above for printing addresses).
c) printf("%d", a[0] + 1); will print the value of the first element of a, plus 1.
C11 draft standard n1570: 6.3.2.1 Lvalues, arrays, and function designators 3 Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’[...]
Thanks to user EOF who pointed out some errors in earlier versions of this answer.
Note: The name of array variable gives its starting address.
printf("%d", a + 1);
So a equals &a[0]. So the above statement is equal to &a[0]+sizeof(a)*1 which equals &a[1]. That is address of a[1].
printf("%d", &a + 1);
This can be approximated to address of address of a[0] + 1 which will be some gibberish.
printf("%d", a[0] + 1);
This is value of a[0]+1.
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].
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).