#include<stdio.h>
void fun(int *);
int main()
{
int n=10;
fun(&n);
printf("%d",n);
return 0;
}
void fun(int *n)
{ int t;
*n++;
t=*n;
}
In function ‘fun’:
pp.c:16: warning: value computed is not used
7 replies to this topic
#1
Posted 30 September 2010 - 02:33 AM
|
|
|
#2
Posted 30 September 2010 - 04:33 AM
It's saying the returned value of the increment was never assigned to anything.
#3
Posted 30 September 2010 - 06:22 AM
I suppose it concerns variable t in the function, which is used only as L-value, and never as part of R-value (L-Value and R-Value Expressions). This warning means that variable t can be removed from source code without affecting the output of the program.
#4
Posted 30 September 2010 - 08:09 AM
Hello to All,
I have modified the code as shown below:- plz correct me if i am wrong
here i am applying the call by reference,hence the changes done in fun() should be reflected in main() printf statement also
but,no luck in fixing the bug
#include<stdio.h>
void fun(int *);
int main()
{
int n=10;
printf("%d",fun(&n));
return 0;
}
void fun(int *n)
{
*n++;
}
gcc -Wall pp.c ---------------shows the following error
compiling it shows the Warning: error: void value not ignored as it ought to be
pp.c: In function ‘fun’:
pp.c:16: warning: value computed is not used
I have modified the code as shown below:- plz correct me if i am wrong
here i am applying the call by reference,hence the changes done in fun() should be reflected in main() printf statement also
but,no luck in fixing the bug
#include<stdio.h>
void fun(int *);
int main()
{
int n=10;
printf("%d",fun(&n));
return 0;
}
void fun(int *n)
{
*n++;
}
gcc -Wall pp.c ---------------shows the following error
compiling it shows the Warning: error: void value not ignored as it ought to be
pp.c: In function ‘fun’:
pp.c:16: warning: value computed is not used
#5
Posted 30 September 2010 - 08:20 AM
On the second look, *n++ is suspicious: ++ operator has higher precedence and it's evaluated before dereferencing operator *. Hence, pointer n is incremented, rather than value pointed to by n. I suppose that your intention was to write (*n)++ or *n = *n + 1 or something like that.
Error is about the way in which you've used fun(&n) in the main function. Function returns void, so its result cannot be printed. Correct code should look like this:
Error is about the way in which you've used fun(&n) in the main function. Function returns void, so its result cannot be printed. Correct code should look like this:
#include<stdio.h>
void fun(int *);
int main()
{
int n=10;
fun(&n);
printf("%d",n);
return 0;
}
void fun(int *n)
{
(*n)++;
}
#6
Posted 30 September 2010 - 06:44 PM
Thank u Zoranh
for the clear-cut explanation
for the clear-cut explanation
#7
Posted 30 September 2010 - 08:02 PM
Do you not need to use a return in the function since it uses a pointer?
#8
Posted 01 October 2010 - 12:31 AM
It's good practice to have a function return value even when it doesn't have to, just to make it more useable. In your example that would be:
int fun(int *n)
{
(*n)++;
return *n;
}
With this modification, you can print the increased value of n as you originally did:
int x = 5;
printf("%d\n", fun(&x));
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









