简体   繁体   English

错误:没有忽略空值,因为它应该在 C 编程中

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

I am writing a C program which has two functions.我正在编写一个具有两个功能的 C 程序。 One function is the usual main function and the other is a pointer void function.一个函数是通常的 main 函数,另一个是指针 void 函数。 When I try to compile my program in a Linux based system I get the following error:当我尝试在基于 Linux 的系统中编译我的程序时,出现以下错误:

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.正如您在上一个问题中提到的, void函数不返回任何内容,因此您不能将其返回值分配给任何内容。 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 ,请像这样定义它:

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应该是这样的:

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. void是一种“无”类型,在表达式中,表示忽略结果,作为返回类型,表示不返回任何内容。 Assigning the (non-existent) return value of a void function doesn't make sense.分配void函数的(不存在的)返回值没有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:C编程中应忽略的空值 - error: Void Value not ignored as it ought to be, C programming C编程中的错误“无效值不应该被忽略” - Error “void value not ignored as it ought to be” in C programming GCC C编译错误,无法忽略void值,因为它应该是 - GCC C compile error, void value not ignored as it ought to be 返回一个int的函数上的C“无效值不应该被忽略”错误 - C “void value not ignored as it ought to be” error on a function that returns an int 错误:无效值不应该被忽略-C / GTK + - error: void value not ignored as it ought to be - C/GTK+ 在C中获得“无效值不应该被忽略” - getting “void value not ignored as it ought to be” in C MINGW编译错误:无法忽略void值,因为它应该是 - MINGW compile error: void value not ignored as it ought to be 警告:取消引用“void *”指针[默认启用]和错误:没有忽略无效值,因为它应该有助于c - warning: dereferencing ‘void *’ pointer [enabled by default] and error: void value not ignored as it ought to be help c 三元运算符用法错误,“ ERROR:应忽略的无效值” - Ternary operator usage error, “ERROR:void value not ignored as it ought to be” 构建驱动程序会产生“错误:没有忽略无效值,因为它应该是” - Building drivers yields `error: void value not ignored as it ought to be`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM