quickie pointer question

This is a discussion on quickie pointer question within the C++ Programming forums, part of the General Programming Boards category; i know this is basic but could someone explaine to me what *p++ does?? does it incriment the value of ...

  1. #1
    1337speaker
    Guest

    quickie pointer question

    i know this is basic but could someone explaine to me what

    *p++ does??

    does it incriment the value of p or .....?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    If p points to an array, you can step through the array using pointer notation:

    *(p + 0)
    *(p + 1)
    *(p + 2)
    ...

    So, if you're in a for loop you could do this:

    *(p + i)

    or if you're in a while loop you could do this:

    *p++

    Behind the scenes you are actually incrementing the address stored in p. The compiler knows the number of bytes required to store one element of the array, and adding 1 to the pointer increments the address in the pointer by that number of bytes. In other words, adding 1 to the pointer moves the pointer to the next element in the array.
    Last edited by 7stud; 04-16-2003 at 03:24 PM.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    *p++ is equivalent to *(p++) because the postincrement/decrement operators (++/--) have a higher priority than the dereference operator (*). So, as 7stud explained, *p++ first returns the value stored at address p, and then increments p (the address, not the contents).

    But you could also write (*p)++. This will return the value stored at address p and then increment the value, so afterwards p will still point to the same memory location but the value stored there will have been increased by 1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 09:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 10:59 AM
  4. A pointer question.
    By joenching in forum C++ Programming
    Replies: 7
    Last Post: 03-20-2008, 04:10 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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