# Thread: i cant understand why i get such an output??

1. ## i cant understand why i get such an output??

Code:
```int array[] = { 45, 67, 89 };    //1st ine
int *array_ptr = &array[1];   //2nd line
printf("&#37;i\n", array_ptr[1]);   //3rd line```
the second line links the pointer *array_ptr with the value of cell "1"
but in the third line it should have print the address of cell "1"
not its value 89

i think that if i want to print cell one we need to change this line into

Code:
`printf("%i\n",*array_ptr[1]);`

why i get the value of cell "1"
when by my logic i should get the adress of cell "1"

??

2. Your logic needs quite a bit of work.

Remember that [] not only adds the offset to the pointer (where the array name points to the first element of the array), but also dereferences. So array points to the first element, array[1] is the value of the second cell, and & gets its address. So array_ptr holds the address of the second cell.

Now the third line adds an offset of 1 to array_ptr, to point to the third cell of the array, and then dereferences to get the value. If you want the address of that value, you'll have to use & -- which should surprise no one since that's exactly the same scenario is on line 2.

3. When you put [] at an array you get the value. If you want the address you just use the pointer. If you want the address of the next element you would do:
Code:
`printf("&#37;i",array_ptr+1);`

4. Following his example it should probably be in this form:
Code:
`printf("&#37;i\n", &array_ptr[1]);   //3rd line`

5. i know that &array_ptr[1] is the address of cell "1"

if we want to print the value of cell one
we need to use *array_ptr[1] (this one points to the value)

but when we just want to print array_ptr[1] we will get the address to which
array_ptr[1] is pointed to

so i cant understand why when we execute this line:
Code:
`printf("&#37;i\n", array_ptr[1]);`
we get the value to which the pointer is pointed to
instead of the address of the cell to which the ponter is pointed too
??

6. Originally Posted by transgalactic2
i know that &array_ptr[1] is the address of cell "1"
Correct (sort of, it's actually the second cell).

Originally Posted by transgalactic2
if we want to print the value of cell one
we need to use *array_ptr[1] (this one points to the value)
Incorrect... array_ptr[1] is the value stored in cell two (array's are zero based so array_ptr[0] is actually the value stored in the "first" cell). array_ptr is a pointer to int, array_ptr[1] is the value in cell one, *array_ptr[1] does not make any sense in this context (maybe if array_ptr was declared as int **array_ptr or int *array_ptr[10] or somesuch it would).

Originally Posted by transgalactic2
but when we just want to print array_ptr[1] we will get the address to which
array_ptr[1] is pointed to
No, as stated before, array_ptr[1] does not represent an address but rather a value at some address. array_ptr+1 on the other hand (or alternately, &array_ptr[1]) represents the address of the second cell.

Originally Posted by transgalactic2
so i cant understand why when we execute this line:
Code:
`printf("%i\n", array_ptr[1]);`
we get the value to which the pointer is pointed to
instead of the address of the cell to which the ponter is pointed too
??
Again, as stated, array_ptr[1] is the value located at an address which is why you get said value as output.

Code:
```int array[4] = { 24, 36, 48, 64 };
int * array_ptr = &array[1];

printf("array_ptr[1] is  : %d\n",array_ptr[1]);
printf("*(array_ptr+1) is: %d\n",*(array_ptr+1));
printf("array_ptr+1 is   : %p\n",array_ptr+1);
printf("&array_ptr[1] is : %p\n\n",&array_ptr[1]);

printf("array[2] is      : %d\n",array[2]);
printf("*(array+2) is    : %d\n",*(array+2));
printf("array+2 is       : %p\n",array+2);
printf("&array[2] is     : %p\n",&array[2]);```
Sample output on my machine:
Code:
```array_ptr[1] is  : 48
*(array_ptr+1) is: 48
array_ptr+1 is   : 0012FF5C
&array_ptr[1] is : 0012FF5C

array[2] is      : 48
*(array+2) is    : 48
array+2 is       : 0012FF5C
&array[2] is     : 0012FF5C```
[/edit]