[lug] c question, int passed by reference

Carl Wagner carl.wagner at verbalworld.com
Mon Oct 8 11:58:43 MDT 2007


Thanks Kenneth, Jeffrey, and Zan. 

The assume what I intended, not what I wrote, filter was not working ;-)

I thought it was something simple but precedence  did not enter my mind. 

Thanks again,
Carl.

as expected,  but 'a' increments the pointer


Zan Lynx wrote:
> On Mon, 2007-10-08 at 10:41 -0600, Carl Wagner wrote:
> [snip]
>   
>> So why aren't the following functions, 'a' and 'b', the same?  'b' works 
>> as expected,  but 'a' increments the pointer
>> This is what I would expect if I did "k++" (without the asterisk).
>>     
> [snip]
>   
>> void a(int *k)
>> {
>>    printf("a1>  k = %u\n", k);
>>    printf("a1> *k = %u\n", *k);
>>    *k++; 
>>    printf("a2>  k = %u\n", k);
>>    printf("a2> *k = %u\n\n", *k);
>>
>> }
>>     
>
> Operator precedence.  It translates it as *(k++) when what you want is
> (*k)++.  Just add parenthesis.
>
>   
> ------------------------------------------------------------------------
>
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: lug.boulder.co.us port=6667 channel=#colug




More information about the LUG mailing list