Thread: Of pointers and arrays

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    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.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jadehippo
    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.

    Quote Originally Posted by jadehippo
    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.
    Last edited by laserlight; 07-14-2009 at 03:46 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    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. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Unless you do some hooky stuff, const pointers aren't supposed to be moved.... you index the pointer instead.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Thank you I forgot you could do that. Silly me, well you saved me lots of debugging time thanks again.

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    5
    shouldnt pointers to the beginning of an array should always be const ?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by lemonwaffles View Post
    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.
    Hope is the first step on the road to disappointment.

  10. #10
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by lemonwaffles View Post
    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).
    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

  11. #11
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    shouldnt pointers to the beginning of an array should always be const ?
    What do you think would be the uses of doing that ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM