Usenet.com

www.Usenet.com

Group Index

Comp Thread Archive from Usenet.com

<-- __Chronological__ --> <-- __Thread__ -->

Re: Incrementing int passed by reference



Ian Bell <[EMAIL PROTECTED]> wrote in message news:<[EMAIL PROTECTED]>...
> I am passing the address of an int into a function.  I want to increment the
> int.  Code snippet
> 
> void foo(int * pc)
> {
>     *pc++;
> }
> 
> pc does not get incremented.  However if I do this:
> 
> foor(int * pc)
> {
>     int temp;
> 
>     temp = *pc;
>     temp++
>     *pc = temp;
> }
> 
> then pc is incremented.
> 
> Is this a funny precedence thing with *?
> 
> Ian

unary * and ++ have the same precedence, however unary operators
associate
from right to left.  You are thus incrementing the pointer in foo. 
You want (*pc)++, or ++*pc, or *pc += 1.

-David
-- 
comp.lang.c.moderated - moderation address: [EMAIL PROTECTED]



<-- __Chronological__ --> <-- __Thread__ -->


Usenet.com



Please check out one of the premium Usenet Newsgroup Service Providers below for access to Usenet.