首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用C指针和结构的链接列表程序中的冲突类型错误消息

使用C指针和结构的链接列表程序中的冲突类型错误消息
EN

Stack Overflow用户
提问于 2017-03-04 22:50:47
回答 2查看 647关注 0票数 0

描述

我试图理解C中的指针、链接列表、结构等等,作为一种学习经验,我编写了一个小程序,它:

  • 定义一些基本结构,并使用结构指针在它们之间创建链接。
  • 遍历整个链接列表并打印出所有变量。
  • 创建另一个结构,但不将其插入链接列表中。
  • 调用一个函数insertEntry,它在链表的一个元素和他的直接追随者之间插入一个给定的结构。

我到目前为止所做的事:

  • This Stack Overflow answer说:“这实际上意味着在源代码结构的其他地方还有另一个函数/声明.有一个不同的函数签名。”
代码语言:javascript
复制
- I've checked for typos in the function definition, declaration and its call.
- I've checked the amount and type of the parameters. Both parameters of `insertEntry` are always two structs of the same type.

===>,我不明白,哪里是我的错误。

ex1_insertStructure_linkedList.h:

代码语言:javascript
复制
void insertEntry(struct entry, struct entry);

ex1_insertStructure_linkedList.c:

代码语言:javascript
复制
#include <stdio.h>
#include "ex1_insertStructure_linkedList.h"

struct entry {
    int value;
    struct entry *next;
};

// clangtidy: conflicting types for 'insertEntry' [clang-diagnostic-error]
void insertEntry(struct entry given_entry, struct entry entry_to_insert) {

    printf("Print inside insertEntry method: %i\n", given_entry.value);
    struct entry *second_pointer = (given_entry).next;

    // entry_to_insert is now the element in the middle
    given_entry.next = &entry_to_insert;

    // the third element
    entry_to_insert.next = second_pointer;

    return;
}

int main(int argc, char *argv[]) {
    struct entry n1, n2, n3;

    n1.value = 1;
    n1.next = &n2;

    n2.value = 32;
    n2.next = &n3;

    n3.value = 34242;
    n3.next = (struct entry *)0;

    struct entry *list_pointer = &n1;

    while (list_pointer != (struct entry *)0) {
        int printValue = (*list_pointer).value;
        list_pointer = (*list_pointer).next;
        printf("%i\n", printValue);
    }

    printf("--------------------\n");
    list_pointer = &n1;

    struct entry a;
    a.value = 999999;
    a.next = (struct entry *)0;

    // clangtidy: argument type 'struct entry' is incomplete [clang-diagnostic-error]
    insertEntry(n1, a);

    while (list_pointer != (struct entry *)0) {
        int printValue = list_pointer->value;
        list_pointer = list_pointer->next;
        printf("%i\n", printValue);
    }

    return 0;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-04 22:59:32

您需要在您的文件entry (即函数声明:void insertEntry(struct entry, struct entry);之前)中“正向声明”结构struct entry;,即在该函数声明之前放置以下struct entry;

之所以出现这种情况,是因为当编译器遇到insertEntry(struct entry, struct entry);时,它对struct entry一无所知。通过向前声明struct entry,您可以“确保”编译器在源文件中的某个位置定义了struct entry

票数 2
EN

Stack Overflow用户

发布于 2017-03-04 22:59:39

您应该在ex1_insertStructure_linkedList.h中放置struct entry声明:

代码语言:javascript
复制
struct entry {
    int value;
    struct entry *next;
};

void insertEntry(struct entry, struct entry);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42602309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档