错误:空值不忽略,因为它应该是在C编程 [英] Error: Void value not ignored as it ought to be in C programming
问题描述
我在写它有两个功能的C程序。一个功能是通常的主要功能,另一种是一个指针void函数。当我尝试编译我的程序在基于Linux的系统,我得到以下错误:
主机1 @矩阵:〜/ cprog /实习> GCC -o功能1 function1.c
prog1.c的:在函数'主':
prog1.c的:16:14:错误:无法忽略,因为它应该是空值
下面是我的code:
的#include<&stdio.h中GT;
无效function_1为(int * NUM1,为int * NUM2);诠释主要(无效){ INT分子;
INT分母;
INT finalAnswer; 的printf(分子:);
scanf函数(%d个,&安培;分子); 的printf(分母);
scanf函数(%d个,&安培;分母); finalAnswer = function_1(安培;分子,和放大器;分母);
的printf(%D /%D =%d个\\ N,分子,分母和放大器; finalAnswer); 返回0;
}无效function_1为(int * NUM1,为int * NUM2){ INT总; 总= * NUM1 / NUM2 *; 返回;
}
作为你的<一提到href=\"http://stackoverflow.com/questions/33243014/how-does-a-void-function-in-c-works-with-pointers\">$p$pvious问题,一个无效
函数返回什么都没有,所以你不能返回值分配给什么。这就是为什么你得到错误。
如果您希望函数返回一个值,但有一个无效
返回类型,定义它是这样的:
无效function_1(INT NUM1,NUM2 INT,INT *总)
{
*总= NUM1 / NUM2;
}
和调用它是这样的:
function_1(分子,分母和放大器; finalAnswer);
另外,你的最后一个的printf
应该是这样的:
的printf(%D /%D =%d个\\ N,分子,分母finalAnswer);
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);
这篇关于错误:空值不忽略,因为它应该是在C编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!