【问题标题】:Conflicting type for function error功能错误的冲突类型
【发布时间】:2015-11-26 15:18:05
【问题描述】:

这是我的第一个 C 程序,我不知道为什么会出现以下错误。

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    struct ListNode* next;
   int content;
} ListNode;

int main() {
    //puts("Hello UPC World"); /* prints Hello UPC World */

    //ListNode* h = malloc(sizeof(ListNode));
    gridinit(3, 5);
    //int c = h->content;
    //printf("%d",c);

    return EXIT_SUCCESS;
}

ListNode* gridinit(int numcolumns, int numrows) {
    ListNode* head = malloc(sizeof(ListNode));
    head->content = 2;
    head->next = NULL;
    return head;
}

为什么我会收到错误提示

func gridinit() 中的类型冲突

【问题讨论】:

  • 您正在从函数返回一个节点,但为什么不将其分配给调用 gridinit 的变量。
  • 看来你需要前向声明在主函数前添加这一行:ListNode *gridinit( int numcolumns, int numrows );
  • 请缩进你的代码

标签: c function implicit-declaration


【解决方案1】:

在结构顶部定义函数声明

ListNode* gridinit(int numcolumns, int numrows);

typedef struct {
    struct ListNode* next;
   int content;
} ListNode;

 int main() {
//puts("Hello UPC World"); /* prints Hello UPC World */

//ListNode* h = malloc(sizeof(ListNode));
gridinit(3, 5);
//int c = h->content;
//printf("%d",c);

return EXIT_SUCCESS;
}

ListNode* gridinit(int numcolumns, int numrows) {
ListNode* head = malloc(sizeof(ListNode));
head->content = 2;
head->next = NULL;
return head;
}

【讨论】:

    【解决方案2】:

    那是因为你的函数是在 被调用之后定义的。您可以在struct 之后和main 之前声明函数的原型为

    ListNode* gridinit(int numcolumns, int numrows);
    

    你会很适合编译。

    【讨论】:

      【解决方案3】:

      TL;DR 答案 - 对于在定义之前使用(调用)的所有函数,您需要在定义之前为这些函数添加 forward declaration调用函数。在这种情况下,由于gridinit() 的定义写在main() 之后,而main() 使用gridinit(),您必须在main() 之前添加gridinit() 的前向声明,这样就可以了。

      为了详细说明情况,在您的 main() 函数中,您正在调用 gridinit(),但在此之前,编译器不知道 gridinit() 函数的原型。

      由于向后兼容,函数的一个称为隐式声明的特性假定在定义或声明之前已经使用过的函数接受任意数量的参数并返回int

      稍后,当您实际定义函数时,您将返回类型设置为ListNode*,这会在此处产生冲突。

      FWIW,根据 C99 标准(以后),隐式函数声明的 (evil) 特性已被正式删除,但编译器仍继续支持遗留代码相同。如果您启用编译器警告并以严格的一致性进行编译,您的编译器应该会收到警告(甚至停止),以防缺少前向声明。

      【讨论】:

        【解决方案4】:

        gridinit()everything works fine 添加forward declaration

        ...
        
        typedef struct {
            struct ListNode* next;
           int content;
        } ListNode;
        
        // Declare the gridinit function
        ListNode* gridinit(int, int);
        
        ...
        

        【讨论】:

          【解决方案5】:

          main 中调用它之前应该有gridinit 的声明或定义(编译器需要知道它存在)。

          此外,您不应忽略gridinit 的返回值,它返回一个您已为其分配内存的指针(忽略它会导致内存泄漏),而应将其分配给h,而不分配内存main(因为您已经在 gridinit 中这样做了)。

          #include <stdio.h>
          #include <stdlib.h>
          
          typedef struct {
              struct ListNode* next;
              int content;
          } ListNode;
          
          ListNode* gridinit(int numcolumns, int numrows) {
          
          int main() {
              //puts("Hello UPC World"); /* prints Hello UPC World */
          
              ListNode* h = gridinit(3, 5);
              int c = h->content;
              printf("%d",c);
          
              return EXIT_SUCCESS;
          }
          
          ListNode* gridinit(int numcolumns, int numrows) {
              ListNode* head = malloc(sizeof(ListNode));
              head->content = 2;
              head->next = NULL;
              return head;
          }
          

          【讨论】:

            猜你喜欢
            • 2016-02-07
            • 2013-03-25
            • 1970-01-01
            • 2014-03-10
            • 2012-10-04
            • 2013-04-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多