# Of pointers and arrays

This is a discussion on Of pointers and arrays within the C Programming forums, part of the General Programming Boards category; I have a pointer pointing to the address of an array, how would i get it so i can change ...

1. ## Of pointers and arrays

I have a pointer pointing to the address of an array, how would i get it so i can change the values stored in the array in regards to the pointer?
Thank you for any help.

2. So, you have a pointer that points to the first element of an array, right? Then dereference the pointer in order to access, and possibly modify (unless the pointer is a pointer to const), the element of the array that the pointer points to. You can then say, increment the pointer to point to the next element in the array, etc.

3. The pointer points to the array (*element = &anarray). This is done in a struct where the array is also created. The changing of values is done in another struct. I don't know if I just point to the first cell (*element = &anarray[0]) if the rest of the array will carry over.

The pointer points to the array (*element = &anarray).
Oh. So think: if the pointer points to the array, then dereferencing the pointer will give you the array. Hence, you can write things like:
Code:
`(*p)[0] = x;`
where p is the pointer and x is some value of (or convertible to) the type of an element of the array.

I don't know if I just point to the first cell (*element = &anarray[0]) if the rest of the array will carry over.
If the pointer points to the first element of the array, then it is not pointing to the array itself. You have to be clear which is which. As I noted earlier, if the pointer points to the first element of the array, you can then access the other elements by pointer arithmetic (and in that sense there is no "carry over", yet it is as if there was). You just need to know how many elements there are, or have some other way of determining where the array ends.

5. I don't know if I just point to the first cell (*element = &anarray[0]) if the rest of the array will carry over.
Arrays are linked in memory. So if you have the address of the first element, you are guaranteed to get the next element if you increment the pointer.

6. Unless you do some hooky stuff, const pointers aren't supposed to be moved.... you index the pointer instead.

7. Thank you I forgot you could do that. Silly me, well you saved me lots of debugging time thanks again.

8. shouldnt pointers to the beginning of an array should always be const ?

9. Originally Posted by lemonwaffles
shouldnt pointers to the beginning of an array should always be const ?
Not if you plan on using that pointer to walk through the array.

Quzah.

10. Originally Posted by lemonwaffles
shouldnt pointers to the beginning of an array should always be const ?
How are you saying this? The pointer can move through the array if allowed(until it's not a const).

11. shouldnt pointers to the beginning of an array should always be const ?
What do you think would be the uses of doing that ?