[英]Error: Semantic issue Assigning to 'int' from incompatible type 'void'
While creating a program with function prototype, a problem has occurred. 使用功能原型创建程序时,出现了问题。 It said: 它说:
Semantic issue Assigning to 'int' from incompatible type 'void'.
Could you please help me resolve this issue? 您能帮我解决这个问题吗?
Here is my code: 这是我的代码:
#include <stdio.h>
#include <math.h>
void powr(int);
int main(void) {
int n=1, sq, cu, quart, quint;
printf("Integer Square Cube Quartic Quintic\n");
do {
sq = powr(n); //here is the error line
cu = powr(n); //here is the error line
quart = powr(n); //here is the error line
quint = powr(n); //here is the error line
printf("%d %d %d %d %d\n", n, sq, cu, quart, quint);
n++;
}
while (n<=25);
return 0;
}
void powr(int n)
{
int a, cu, quart, quint;
a=pow(n,2);
cu=pow(n,3);
quart=pow(n,4);
quint=pow(n,2);
}
void powr(int n)
means that the function will return nothing, so you're not allowed to do something like: 表示该函数将不返回任何内容,因此不允许您执行以下操作:
sq = powr(n);
If you want your function to take an int
and return an int
, it should be: 如果要让函数采用int
并返回 int
,则应为:
int powr(int n)
(for both the prototype and the function definition). (对于原型和函数定义)。
In any case, variables that you set within the powr
function are not available to the caller (and using globals is a very bad idea in general), so you'd need to either change the function to return just the square of the number and call it thus: 无论如何,您在 powr
函数中设置的变量都不可供调用者使用(通常,使用globals是一个非常糟糕的主意),因此您需要更改函数以仅返回数字的平方和这样称呼它:
sq = powr (n);
cu = n * sq;
quart = powr (sq);
quint = n * quart;
Or you could pass the addresses of the variables into the function so they could be changed, something like: 或者,您可以将变量的地址传递给函数,以便可以对其进行更改,例如:
void powr(int n, int *pSq, int *pCu, int *pTo4, int *pTo5) {
*pSq = pow (n, 2);
*pCu = *pSq * n;
*pTo4 = pow (*pSq, 2);
*pTo5 = *pCu * *pSq;
}
and call it with: 并调用:
powr (n, &sq, &cu, &quart, &quint);
I would suggest using the former approach, given the level you appear to be learning at (no offence intended, just stating that to assist you in selecting the appropriate method). 考虑到您似乎正在学习的水平,我建议使用前一种方法(没有冒犯的意图,只是说这是为了帮助您选择适当的方法)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.