cla1n
cla1n

Reputation: 39

Printf and Array

I was asked this question as a class exercise:

int A[] = {1,3,5,7,9,0,2,4,6};

printf("%d\n", *(A+A[1]-*A));

I couldn't figure it out on paper, so went ahead to compiling a simple program and tested it and found that printf("%d",*A) always gives me 1 for the output.

But I still do not understand why this is the case, hence it would be great if someone can explain this.

Upvotes: 3

Views: 314

Answers (4)

MK.
MK.

Reputation: 34587

A is treated like a pointer to the first element of array of integers. A[1] is the value of the first element of that array, which is 3 (indexes are 0-based) *A is the value to which A points, which if the zeroth element of array, so 1. So

A[1] - *A == 3 - 1 == 2

Now we have

*(A + 2)

That's where pointer arithmetic kicks in. Since A is a pointer to integer, A+2 points to the second (0-based) item in that array and *(A+2) gets its value. So answer is 5.

Also please note for future reference that pointer to an integer and array of integers are somewhat different things in C, but for the purposes of this discussion they are the same thing.

Upvotes: 7

MikeCAT
MikeCAT

Reputation: 75062

  • Arrays used in expressions are automatically converted into pointers pointing at the first elements of the arrays except for some exceptions such as operands of sizeof or unary & operators.
  • E1[E2] is defined to be equivalent to *((E1) + (E2))
  • + and - operator used to pointers will move the pointer forward and backward.

In this case, *A is equivalent to *(A + 0), which is equivalent to A[0] and it will give you the first element of the array.

The expression *(A+A[1]-*A) will

  1. Get the pointer to the first element, which points at 1, via A
  2. Move the pointer to A[1] (3) elements ahead via +A[1], so the pointer now points at 7
  3. Move the pointer to *A (1) element before what is pointed via -*A, so the pointer now points at 5
  4. Dereference the pointer via the unary * operator, so the expression is evaluated to 5

Upvotes: 2

Remy Lebeau
Remy Lebeau

Reputation: 597951

Break it down into its constituent parts:

A by itself is the memory address of the array, which is also equivalent to &A[0], the memory address of the first element of the array.

A[1] is the value stored in the second element of the array, which is 3.

*A dereferences the memory address of the array, which is equivilent to A[0], the value stored in the first element of the array, which is 1.

So, do some substitutions:

  *(A+A[1]-*A)
= *(A+(A[1])-(A[0]))
= *(A+3-1)
= *(A+2)

The notation *(Array+index) is the same as the notation Array[index]. Under the hood, they both take the starting address of the array, increment it by the number of bytes of the array element type (in this case, int) multiplied by the index, and then dereference the resulting address. So *(A+2) is the same as A[2], which is 5.

Upvotes: 4

learning_frog
learning_frog

Reputation: 325

An array variable in C is only the pointer to the initial memory location for the array. So if you derreference the array, you will always get the value for the first position.

If you sum up 1 to the original array value, like *(A+1) you will get the second position.

You can get any position from the array using the same method:

*(A) is the first position

*(A+1) is the second position

*(A+2) is the third position

and so on...

If you declare the int array as int* A and allocate the memory and attribute the values, it is usually easier to visualize how this works.

Upvotes: 0

Related Questions