[英]Error: Semantic issue Assigning to 'int' from incompatible type 'void'
使用功能原型创建程序时,出现了问题。 它说:
Semantic issue Assigning to 'int' from incompatible type 'void'.
您能帮我解决这个问题吗?
这是我的代码:
#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)
表示该函数将不返回任何内容,因此不允许您执行以下操作:
sq = powr(n);
如果要让函数采用int
并返回 int
,则应为:
int powr(int n)
(对于原型和函数定义)。
无论如何,您在 powr
函数中设置的变量都不可供调用者使用(通常,使用globals是一个非常糟糕的主意),因此您需要更改函数以仅返回数字的平方和这样称呼它:
sq = powr (n);
cu = n * sq;
quart = powr (sq);
quint = n * quart;
或者,您可以将变量的地址传递给函数,以便可以对其进行更改,例如:
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;
}
并调用:
powr (n, &sq, &cu, &quart, &quint);
考虑到您似乎正在学习的水平,我建议使用前一种方法(没有冒犯的意图,只是说这是为了帮助您选择适当的方法)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.