Why do I get "Value computed is not used" when working with pointers?

摘自:http://tigcc.ticalc.org/doc/faq.html#99


Q:    I have a variable and a pointer to it, for example,


int a, ptr_to_a = &a;

When I tried to modify the variable "a" indirectly using the pointer, like in

*ptr_to_a++;

the compiler reports to me "Value computed is not used". What is wrong here?


A:    Note that although operators '++' and '*' have the same precedence, '++' will be evaluated first, so this expression will be evaluated as

*(ptr_to_a++);

i.e. it increases the pointer, then reads the value from it (which is not used for anything). This is not what do you want, of course. To perform what do you want (i.e. to increase the variable pointed to by the pointer), use parentheses to change the order of evaluation, i.e. use

(*ptr_to_a)++;

This will work as expected.


posted @ 2012-03-19 15:43  wdliming  阅读(202)  评论(0编辑  收藏  举报