Error: Void value not ignored as it ought to be in C programming

前端 未结 2 818
青春惊慌失措
青春惊慌失措 2021-01-29 07:11

I am writing a C program which has two functions. One function is the usual main function and the other is a pointer void function. When I try to compile my program in a Linux

相关标签:
2条回答
  • 2021-01-29 07:59

    This:

    void function_1(int *num1, int *num2)
    

    returns nothing. void is kind of a "nothing" type, in an expression, it means to ignore the result, as a return type, it means nothing is returned. Assigning the (non-existent) return value of a void function doesn't make sense.

    0 讨论(0)
  • 2021-01-29 08:08

    As mentioned in your previous question, a void function returns nothing, so you can't assign its return value to anything. That's why you're getting the error.

    If you want the function to send back a value but have a void return type, define it like this:

    void function_1(int num1, int num2, int *total) 
    {
        *total = num1 / num2;
    }
    

    And call it like this:

    function_1(numerator, denominator, &finalAnswer);
    

    Also, your final printf should be this:

    printf("%d / %d = %d \n", numerator,denominator,finalAnswer);
    
    0 讨论(0)
提交回复
热议问题