C 为什么我从不兼容的指针类型警告中得到初始化?

C 为什么我从不兼容的指针类型警告中得到初始化?,c,initialization,warnings,C,Initialization,Warnings,因此,我在Linux上使用gcc,并在单独的文件中包含以下两个代码段(仅包含相关的代码部分): 程序编译和运行正常,但在编译过程中,我收到警告: problem_003.c: In function ‘main’: problem_003.c:7:26: warning: initialization from incompatible pointer type [enabled by default] int_pair_list *decomp = prime_decomp(N);

因此,我在Linux上使用gcc,并在单独的文件中包含以下两个代码段(仅包含相关的代码部分):

程序编译和运行正常,但在编译过程中,我收到警告:

problem_003.c: In function ‘main’:
problem_003.c:7:26: warning: initialization from incompatible pointer type [enabled by default]
  int_pair_list *decomp = prime_decomp(N);
                      ^

我是C语言的新手,我不知道为什么会收到这个警告。

在C语言中,函数(或它的原型)应该在使用之前声明,以便确定它的正确签名。否则,编译器将尝试“猜测”正确的签名。虽然它可以从调用中推断参数类型,但返回值类型并非如此,返回值类型默认为
int
。在代码中,函数在使用之前不是原型,因此编译器假定它返回
int
。这就是它警告您类型分配不兼容的原因。

函数是否在main之前声明?
int\u pair\u list*head=malloc(sizeof(*head))
?或者
int\u pair\u list*head=malloc(sizeof(head))
?看到行号,我认为没有声明原型。那么,我想就是这样了…@itsnotmyrealname没有,
sizeof(*head)
是正确的。这可能会解决它。我知道我修改了函数的返回类型,考虑到这一点,我忘了用is声明更新头文件。谢谢大家的回复。是的,修复了。在要求我的计算机内存过多(300425737571 int:/)后,我不得不等待它停止崩溃。
int_pair_list *prime_decomp(unsigned int n)
{
    int_pair_list *head = malloc(sizeof(*head));
    int_pair_list *current;
    /* method body removed, current and head remain as int_pair_list pointers */
    return current ? head : NULL;
}
problem_003.c: In function ‘main’:
problem_003.c:7:26: warning: initialization from incompatible pointer type [enabled by default]
  int_pair_list *decomp = prime_decomp(N);
                      ^