[英]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.