[lug] c question, int passed by reference

Kenneth D Weinert kenw at quarter-flash.com
Mon Oct 8 11:15:45 MDT 2007


In a word:  precedence

in a() change *k++  to (*k)++ and all will be fine.  Take a look at the
precedence tables to see why this is so.


On Mon, 2007-10-08 at 10:41 -0600, Carl Wagner wrote:
> #include <stdio.h>
> 
> void a(int *k);
> void b(int *l);
> 
> int
> main(void)
> {
>    int i,j,x;
>  
>    i = 3;
>    a(&i);
> 
>    j = 7;
>    b(&j);
> }
> 
> 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);
> 
> }
> 
> void b(int *l)
> {
>    printf("b1>  l = %u\n", l);
>    printf("b1> *l = %u\n", *l);
>    *l = *l +1;
>    printf("b2>  l = %u\n", l);
>    printf("b2> *l = %u\n", *l);
> 
> }
> 
> 
-- 
Ken Weinert
http://quarter-flash.com

Please avoid sending me Word or PowerPoint attachments
http://www.gnu.org/philosophy/no-word-attachments.html
-------------- 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/3a6ac476/attachment.pgp>


More information about the LUG mailing list