# Thread: Pointer assignment using array name

1. ## Pointer assignment using array name

Could someone please explain why the pointer ptr assignment causes ptr to point at one memory location past the array last element?

I have output the memory locations just for verification. I thought that &a could point to the first element of the array.

0022FF60 0022FF64 0022FF68 0022FF6C
0022FF70
4 3

Code:
```#include <stdio.h>

int main(void)
{
int  a[4] = {1,2,3,4};
int *ptr = (int*)(&a+1); // Why does this assignement put the ptr pointer
// to point at one memory location after the array???
int *p = a;

printf("%p %p %p %p\n",p,(p+1),(p+2),(p+3));
printf("%p \n",ptr);
printf("%d %d", *(a+3), *(ptr-2) );

return 0;
}```

2. Actually,you're changing the value of the address of an array a with the following statement in your program.

Code:
`int *ptr = (int*)(&a+1);`
The above statement makes the pointer of an array which address is 2000 will be changed to 2016 where no element points.The value is incremented by 16 because the array itself can store four elements which is 4*4=16.That's why it is giving garbage value.If you want the pointer to point to first element of the array.You use the following code.

Code:
`int *ptr=(int*)(&a);`
If you want the pointer to point to second element of the array.You use the following code.

Code:
`int *ptr=(int*)(&a)+1;`

What is the difference of

Code:
`int *ptr = (int*)(&a+1);`
and that of

Code:
`int *ptr = (int*)(&a)+1;`
since the & operator has a higher priority of the + operator?

If taking the &a, we take tha address of the array (actuallly the address of the first element), then this address plus one should point to the second element, right?

Why does it point to the next memory location past the array?

4. Code:
`int *ptr = (int*)(&a+1);`
The above statement will take the size of an array which is 16 bytes and when it is incremented by 1,16 bytes will be added to it.

In the case of second statement,

Code:
`int *ptr = (int*)(&a)+1;`
The above statement will take the size of an integer pointer which is 4 bytes not the size of an array which is 16 bytes.When it is incremented by 1,4 bytes will be added to it.So,it will point to the second element of the array because expressions within () will be executed first.

5. Ok, I understand.

I had not seen up to now such a definition where you can take the whole size of an array (besides "sizeof" operator) and add a multiple integral of such size to it.

This is what confused me.

This was an exercise I saw and tried to solve. I wonder if someone can actually take advantage of such assignments in a real program.

Why should someone want to add multiple times the size of an array??

6. Actually, if you want a pointer to point to the second element of the array a, you should write:
Code:
`int *ptr = a + 1;`
This is because an array is converted to a pointer to its first element. Adding 1 to such an array results in a pointer to the second element.

&a really does give you the address of the array a. It so happens that this address is equal in value to the address of the first element of a, but in terms of type the pointers are not the same, i.e., &a is an int(*)[4], whereas a when converted to a pointer to its first element is an int*.

Originally Posted by stavos77
This was an exercise I saw and tried to solve. I wonder if someone can actually take advantage of such assignments in a real program.

Why should someone want to add multiple times the size of an array??
If you are dealing with a two dimensional array, then traversing the elements of such an array involves traversing over arrays.

7. Laserlight you are great.

Now I really understand that. "&a" and "a" are pointing to the same element (first of array) but they are different types of pointers.

Multidimensional arrays could certainly use this technique...you are right.

Thanks

8. Originally Posted by stavos77
Now I really understand that. "&a" and "a" are pointing to the same element (first of array) but they are different types of pointers.
No.

The element pointed to by &a is the array. So (&a + 1) points past the end of the array.

The element pointed by by a is the first element of the array (i.e. the int with a value 1). So a + 1 points to the second element of the array (i.e. the int with a value 2).

The fact the code won't compiler unless you use a conversion in this line
Code:
`   int *p = (int *) (&a + 1);`
tells you that &a + 1 is not a pointer to an int.