[lug] c question, int passed by reference

Zan Lynx zlynx at acm.org
Mon Oct 8 11:19:52 MDT 2007


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.

-- 
Zan Lynx <zlynx at acm.org>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://lists.lug.boulder.co.us/pipermail/lug/attachments/20071008/db25c7bd/attachment.pgp>


More information about the LUG mailing list