简体   繁体   English

错误:语义问题从不兼容类型“ void”分配给“ int”

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

相关问题 从不兼容的void *中分配int * - Assigning int* from incompatible void * 为什么我从“不兼容类型&#39;void *&#39;”错误中“分配&#39;int *&#39;? - Why do I get “assigning to 'int *' from incompatible type 'void *' ” error? 错误:从“void *”类型分配类型“值”时出现不兼容的类型? - Error: incompatible types when assigning to type 'values' from type 'void *'? 从可可项目中的不兼容类型“ void *”错误分配给“ MyPrivateData *” - Assigning to “MyPrivateData *” from incompatible type 'void *' error in cocoa project 从“int (int *)”分配给“void (*)(void *)”的不兼容指针类型 - incompatible pointer types assigning to 'void (*)(void *)' from 'int (int *)' 从不兼容的类型&#39;int&#39;分配[custom typdef] - Assigning to [custom typdef] from incompatible type 'int' test.c:51:4:错误:从类型&#39;void *&#39;分配给类型&#39;blk时,类型不兼容 - test.c:51:4: error: incompatible types when assigning to type ‘blk from type ‘void *’ 错误:分配给类型 'BusRoute' {aka 'struct 时的类型不兼容<anonymous> '} 来自类型 'void *'</anonymous> - error: incompatible types when assigning to type ‘BusRoute’ {aka ‘struct <anonymous>’} from type ‘void *’ 编译器错误:在malloc期间从类型&#39;void *&#39;分配给&#39;struct&#39;时出现不兼容的类型 - Compiler error: incompatible types when assigning to 'struct' from type 'void *' during malloc 从类型int分配类型char [2]时编译“不兼容类型”的错误 - compile error of "incompatible types when assigning to type char[2] from type int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM