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. #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
    21,658
    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

  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
    21,658
    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.
    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

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    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, 03:24 PM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 04:31 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21