Increment the address stored in a pointer???

This is a discussion on Increment the address stored in a pointer??? within the C++ Programming forums, part of the General Programming Boards category; Hi gang, My text is telling this can be done, but I do not see any mention of it in ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    121

    Increment the address stored in a pointer???

    Hi gang,

    My text is telling this can be done, but I do not see any mention of it in this particular chapter. Is there indeed a way to modify the actual address stored in a pointer? In this case, it is a pointer to an array. What is VERY strange is that if I pass a char array to a function, then inside that function,

    pointerName++;

    is valid, and seems to increment. However, this doesn't work in int main(). Any ideas why?

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Nevermind, it didn't increment it, it just output differently while in the function (had a different address while in the function than in main()- WEIRD!!!). Any ideas on why

    pointerName++

    gives me an error in main(), but not inside a function? So, can an address in a pointer be incremented?

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    For help with real code, post real code.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    I wouldn't say I need help with code, I just can't think of how to increment the address stored in a pointer variable. Isn't it fixed once it is set?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by patricio2626 View Post
    I wouldn't say I need help with code, I just can't think of how to increment the address stored in a pointer variable. Isn't it fixed once it is set?
    I would say it depends on the code.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    > gives me an error in main(), but not inside a function? So, can an address in a pointer be incremented?
    Now that sounds like you tried to increment an array name, not a pointer (to the array) inside another function.

    Dave's right, post actual code and actual error messages.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Do you want to increment the memory address or the value that the pointer points to?

    Kind of odd you want to change the memory address.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    [edit]
    Last edited by patricio2626; 04-04-2007 at 07:55 PM.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    I think what's not working for you is this:
    Code:
    void func(char array[]){
         ++array;//will compile fine
    }
    
    int main(){
        char array[5]={0}
        array++;//Will not compile
        func(array);//will compile
    }
    The difference is that when passed to a function, the function recieves a a copy of the array pointer. This copy is mutable, but the orignal is not.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    OK, that explains it, King Mir! I thought that the function received the actual array itself. Thanks for the help, that explains it!

    -Patrick

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Pointers" <-- crappy name
    By argv in forum General Discussions
    Replies: 64
    Last Post: 06-25-2009, 09:18 PM
  2. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 10:48 AM
  3. I am very new . . . :(
    By Eternalglory47 in forum C++ Programming
    Replies: 6
    Last Post: 09-05-2008, 12:29 PM
  4. Should i pass address of pointer or just pointer???
    By howhy in forum C++ Programming
    Replies: 11
    Last Post: 09-02-2005, 05:05 AM
  5. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 02:09 PM

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