C - .h和.c文件中的冲突类型

时间:2016-02-12 00:42:34

标签: c

我收到error: conflicting types for 'initList'的错误,我在.c文件和.h文件中写了一些代码。这是.c文件中的代码:

#include "lists.h"

void initList(LIST* list) {
  list->head.coef = 0;
  list->head.exp = 0;
}

这是.h文件中的相应代码:

#ifndef LISTS_H__
#define LISTS_H__

#include <stdio.h>
#include <string.h>

typedef struct node {
  int coef;
  int exp;
  struct node* next;
} NODE;

typedef struct list {
  NODE head;
} LIST;

void initList(LIST*);

#endif

我真的不明白我在这里做错了什么......我已经检查了其他类似的问题,但没有一个解决方案,他们的答案建议在这里工作和/或我已经有了修复工作。

1 个答案:

答案 0 :(得分:0)

首先,变量的单词列表不是一个很好的选择。继续前进。

从我所看到的看来,你正在创建一个自引用结构,尽管head元素是一个单独的结构,但结构与节点相同。

与某些评论一样,我也没有对您发布的代码产生任何错误,所以为了帮助您,这里有一些代码使用您创建的列表然后添加值并链接列表和节点在一起。我还使用 - &gt;对最后一个节点进行了更改。操作

我希望这可以帮助你。

在h文件中。

/*
 *  35352860_list.h
 */

#ifndef _35352860_LIST_H__
#define _35352860_LIST_H__

#include <stdio.h>
#include <string.h>

typedef struct node
    {
      int coef;
      int exp;
      struct node* next;
    } NODE;

typedef struct list
    {
    NODE head;
    } LIST;

void initList(LIST*);

#endif  /* _35352860_LIST_H__ */

和c档。

    /*
 *  35352860_main.c
 */

#include "35352860_list.h"

void initList(LIST* list)
    {
    list->head.coef = 0x0000;
    list->head.exp = 0x0000;
    list->head.next = NULL;
    }


int main
    (
    unsigned int    argc,
    unsigned char   *arg[]
    )
{
    LIST    list;
    NODE    node_1;
    NODE    node_2;

    printf("There is 1 variable of type LIST and 2 variable of type NODE.\n");
    printf("Address of list................................0x%.8X\n", &list);
    printf("Value of list.head.coef........................0x%.8X\n", list.head.coef);
    printf("Value of list.head.exp.........................0x%.8X\n", list.head.exp);
    printf("Value of list.head.next........................0x%.8X\n", list.head.next);
    printf("The nodes                                                Node_1      Node_2\n");
    printf("Address of nodes.........................................0x%.8X  0x%.8X\n", &node_1, &node_2);
    printf("Value of nodes.coef......................................0x%.8X  0x%.8X\n", node_1.coef, node_2.coef);
    printf("Value of nodes.exp.......................................0x%.8X  0x%.8X\n", node_1.exp, node_2.exp);
    printf("Value of nodes.next......................................0x%.8X  0x%.8X\n", node_1.next, node_2.next);
    printf("\n\nCall initList.\n");
    initList(&list);
    printf("Address of list.head...........................0x%.8X\n", &list);
    printf("Value of list.head.coef........................0x%.8X\n", list.head.coef);
    printf("Value of list.head.exp.........................0x%.8X\n", list.head.exp);
    printf("Value of list.head.next........................0x%.8X\n", list.head.next);
    printf("The nodes                                                Node_1      Node_2\n");
    printf("Address of nodes.........................................0x%.8X  0x%.8X\n", &node_1, &node_2);
    printf("Value of nodes.coef......................................0x%.8X  0x%.8X\n", node_1.coef, node_2.coef);
    printf("Value of nodes.exp.......................................0x%.8X  0x%.8X\n", node_1.exp, node_2.exp);
    printf("Value of nodes.next......................................0x%.8X  0x%.8X\n", node_1.next, node_2.next);
    printf("\n\nLinking node_1 to list, and add some values to node_1.\n");
    list.head.next = &node_1;
    node_1.coef = 0x1111;
    node_1.exp = 0x1111;
    node_1.next = NULL;
    printf("Address of list.head...........................0x%.8X\n", &list);
    printf("Value of list.head.coef........................0x%.8X\n", list.head.coef);
    printf("Value of list.head.exp.........................0x%.8X\n", list.head.exp);
    printf("Value of list.head.next........................0x%.8X\n", list.head.next);
    printf("The nodes                                                Node_1      Node_2\n");
    printf("Address of nodes.........................................0x%.8X  0x%.8X\n", &node_1, &node_2);
    printf("Value of nodes.coef......................................0x%.8X  0x%.8X\n", node_1.coef, node_2.coef);
    printf("Value of nodes.exp.......................................0x%.8X  0x%.8X\n", node_1.exp, node_2.exp);
    printf("Value of nodes.next......................................0x%.8X  0x%.8X\n", node_1.next, node_2.next);
    printf("\n\nLinking node_2 to node_1, and add some values to node_2.\n");
    node_1.next = &node_2;
    node_2.coef = 0x2222;
    node_2.exp = 0x2222;
    node_2.next = NULL;
    printf("Address of list.head...........................0x%.8X\n", &list);
    printf("Value of list.head.coef........................0x%.8X\n", list.head.coef);
    printf("Value of list.head.exp.........................0x%.8X\n", list.head.exp);
    printf("Value of list.head.next........................0x%.8X\n", list.head.next);
    printf("The nodes                                                Node_1      Node_2\n");
    printf("Address of nodes.........................................0x%.8X  0x%.8X\n", &node_1, &node_2);
    printf("Value of nodes.coef......................................0x%.8X  0x%.8X\n", node_1.coef, node_2.coef);
    printf("Value of nodes.exp.......................................0x%.8X  0x%.8X\n", node_1.exp, node_2.exp);
    printf("Value of nodes.next......................................0x%.8X  0x%.8X\n", node_1.next, node_2.next);
    printf("\n\nChange the value node_2.coef using the -> operator.\n");
    list.head.next -> next -> coef = 0x3333;
    printf("Address of list.head...........................0x%.8X\n", &list);
    printf("Value of list.head.coef........................0x%.8X\n", list.head.coef);
    printf("Value of list.head.exp.........................0x%.8X\n", list.head.exp);
    printf("Value of list.head.next........................0x%.8X\n", list.head.next);
    printf("The nodes                                                Node_1      Node_2\n");
    printf("Address of nodes.........................................0x%.8X  0x%.8X\n", &node_1, &node_2);
    printf("Value of nodes.coef......................................0x%.8X  0x%.8X\n", node_1.coef, node_2.coef);
    printf("Value of nodes.exp.......................................0x%.8X  0x%.8X\n", node_1.exp, node_2.exp);
    printf("Value of nodes.next......................................0x%.8X  0x%.8X\n", node_1.next, node_2.next);

    return(0x0000);
}