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