Why do I get "Value computed is not used" when working with pointers?
2012-03-19 15:42
615 查看
摘自: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.
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.
相关文章推荐
- Attribute value request.getParameter("name") is quoted with " which must be escaped when used
- Attribute value request.getParameter("digyso") is quoted with " which must be escaped when used within the value
- Attribute value request.getAttribute("keyWord") is quoted with " which must be escaped when used wit
- 问题解决: Attribute value request.getParameter("id") is quoted with " which must be escaped when used wi
- 问题解决: Attribute value request.getParameter("id") is quoted with " which must be escaped when used within the value
- Attribute value request.getParameter("itemID") is quoted with " which must be escaped when used with
- 问题解决: Attribute value request.getParameter("id") is quoted with " which must be escaped when used wi
- Attribute value request.getAttribute("keyWord") is quoted with " which must be escaped when used wi
- is quoted with " which must be escaped when used within the value
- 错误:is quoted with " which must be escaped when used within the value
- Attribute value is quoted with " which must be escaped when used within the value 问题解决
- 错误:is quoted with " which must be escaped when used within the value
- 关于The function get must be used with a prefix when a default namespace is not specified的错误
- 错误:is quoted with " which must be escaped when used within the value .
- The function getUserId must be used with a prefix when a default namespace is not specified 解决办法
- The function getUserId must be used with a prefix when a default namespace is not specified 解决办法
- JasperException: is quoted with " which must be escaped when used within the value
- JasperException: is quoted with " which must be escaped when used within the value
- Attribute value ("XXX") is quoted with " which must be escaped when used within the value 异常的修正
- Attribute value "xx" is quoted with " which must be escaped when used within the