what is the difference between them..
*(p++) and *(++p)
what is the difference between them..
*(p++) and *(++p)
Whether there is a net difference depends on context. What do you think is the difference?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i think there is no difference
in both cases it points to the next address
i gives the value of the next cell
Observe the output of this program and draw your conclusions:Originally Posted by transgalactic2
Code:#include <stdio.h> int main(void) { int numbers[2] = {1, 2}; int *p = numbers; printf("%d\n", *(p++)); printf("%d\n", *p); p = numbers; printf("%d\n", *(++p)); printf("%d\n", *p); return 0; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
it against the logic of cols
i used that the cols are separating
so the part inside the cols are separated from the operation outside the cols.
but here
it returns the value inside p goes to printf and then return back to the cols and increases the address by 1.Code:printf("%d\n", *(p++));
why???
it should be like an onion
do whats inside the cols the go outside
like in if statement
if ((a)&&(b)) etc..
i evaluates "a" and "b" anf then compares them like onion
??
The result of p++ is p. Therefore, the result of *(p++) is *p. The increment is a side effect. Incidentally, *(p++) is equivalent to *p++.Originally Posted by transgalactic2
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
so *(p++) says
first do the operation outside and then go back and increase p by 1
correct?
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
"first take the current value of p, then increment p" and then look at the address of the expression."
"first take the current value of p"
means go with the first value to the printf
then "increment p"
p has increased address
correct?
What *(p++) means, is this:
Increment the position in memory that p points to, so, if it originally pointed to element 25 of an array, increment p to point to element 26. However, since the ++ is on the right side, increment it *after* accessing the memory. *(++p) is slightly different.
Example program:
Example program output:Code:#include <stdio.h> int main() { int array[5] = {0, 2, 4, 6, 8}; int *p = &array[1]; printf("%i\n", *(p++)); printf("%i\n", *p); return 0; }
However, another example, this time for *(++p):Code:2 4
The output this time isCode:#include <stdio.h> int main() { int array[5] = {0, 2, 4, 6, 8}; int *p = &array[1]; printf("%i\n", *(++p)); printf("%i\n", *p); return 0; }
Code:4 4
Last edited by Nightowl; 03-19-2009 at 01:31 PM.
Do as I say, not as I do . . .
Experimentation is the essence of programming. Just remember to make a backup first.
"I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF
Questions posted by these guidelines are more likely to be answered.
Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.