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 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:
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).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.