Jump to content

please help in understanding the Warning

- - - - -

  • Please log in to reply
7 replies to this topic

#1
ahshan

ahshan

    Newbie

  • Members
  • Pip
  • 8 posts
#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

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
It's saying the returned value of the increment was never assigned to anything.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
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
ahshan

ahshan

    Newbie

  • Members
  • Pip
  • 8 posts
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

#5
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
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:
#include<stdio.h>

void fun(int *);

int main()
{
    int n=10;
    fun(&n);
    printf("%d",n);
    return 0;
}

void fun(int *n)
{
    (*n)++;
} 


#6
ahshan

ahshan

    Newbie

  • Members
  • Pip
  • 8 posts
Thank u Zoranh

for the clear-cut explanation

#7
mrlemke

mrlemke

    Learning Programmer

  • Members
  • PipPipPip
  • 68 posts
  • Location:Redding, CA
Do you not need to use a return in the function since it uses a pointer?

#8
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
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