简体   繁体   中英

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

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 based system I get the following error:

host1@matrix:~/cprog/practice> gcc -o function1 function1.c
prog1.c: In function ‘main’:
prog1.c:16:14: error: void value not ignored as it ought to be

Here is my code:

#include <stdio.h>
void function_1(int *num1, int *num2);

int main(void) {

    int numerator;
    int denominator;
    int finalAnswer;

    printf("Numerator: ");
    scanf("%d", &numerator);

    printf("Denominator: ");
    scanf("%d", &denominator);

    finalAnswer = function_1(&numerator, &denominator);
    printf("%d / %d = %d \n", numerator,denominator,&finalAnswer);

    return 0;
}

void function_1(int *num1, int *num2) {

    int total;

    total = *num1 / *num2;

    return;
}

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM