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.
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 ...
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.
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.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
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.
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:Originally Posted by jadehippo
where p is the pointer and x is some value of (or convertible to) the type of an element of the array.Code:(*p)[0] = x;
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.Originally Posted by jadehippo
Last edited by laserlight; 07-14-2009 at 03:46 PM.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
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.I don't know if I just point to the first cell (*element = &anarray[0]) if the rest of the array will carry over.
Unless you do some hooky stuff, const pointers aren't supposed to be moved.... you index the pointer instead.
Originally Posted by phantomotap
Thank youI forgot you could do that. Silly me, well you saved me lots of debugging time
thanks again.
shouldnt pointers to the beginning of an array should always be const ?
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
What do you think would be the uses of doing that ?shouldnt pointers to the beginning of an array should always be const ?