i know this is basic but could someone explaine to me what
*p++ does??
does it incriment the value of p or .....?
i know this is basic but could someone explaine to me what
*p++ does??
does it incriment the value of p or .....?
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.
*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.