Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-跨.h和.C文件的冲突类型_C - Fatal编程技术网

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

C-跨.h和.C文件的冲突类型,c,C,我得到一个错误error: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 &l

我得到一个错误
error: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
\ifndef列表__
#定义列表__
#包括
#包括
类型定义结构节点{
内系数;
国际贸易;
结构节点*下一步;
}节点;
类型定义结构列表{
节点头;
}名单;
无效初始列表(列表*);
#恩迪夫

我真的不明白我做错了什么。。。我已经检查了其他类似的问题,但是他们的答案所建议的解决方案在这里都不起作用,而且/或者我已经有了相应的解决方案。

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

从我所看到的情况来看,您似乎正在创建一个自引用结构,尽管head元素是一个单独的结构,但与节点具有相同的结构

像一些评论一样,我也没有发现您发布的代码有任何错误,因此为了帮助您,这里有一些代码使用您必须创建的列表,然后添加值,并将列表和节点链接在一起。我还使用->操作符更改了最后一个节点

我希望这对你有帮助

在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编译器编译还是使用C++编译器?我在Windows上使用MinGW的GCC。如果你把参数名(list)添加到声明中会怎么样?Jerry Jeremiah:这是不现实的。这可能是其他问题(与宏观经济相关或类似问题)的副作用,但它本身并不是问题。C编译器不会将变量名
list
与结构标记
list
@Jerry Jeremiah混淆:
节点头
行显然是复制粘贴错误。编译器试图指出前面的
initList
声明。它不可能为此引用
节点头
行。同样,所提供的代码完全没有问题。问题的根源在于我们看不到的代码的其余部分。删除struct标记只能掩盖问题,它并不能真正解决任何问题。
    /*
 *  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);
}