描述
我试图理解C中的指针、链接列表、结构等等,作为一种学习经验,我编写了一个小程序,它:
insertEntry,它在链表的一个元素和他的直接追随者之间插入一个给定的结构。我到目前为止所做的事:
- 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.
#include "client.h"的定义”,但我也查过了。文件系统和#include上的实际文件名。===>,我不明白,哪里是我的错误。
ex1_insertStructure_linkedList.h:
void insertEntry(struct entry, struct entry);ex1_insertStructure_linkedList.c:
#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;
}发布于 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。
发布于 2017-03-04 22:59:39
您应该在ex1_insertStructure_linkedList.h中放置struct entry声明:
struct entry {
int value;
struct entry *next;
};
void insertEntry(struct entry, struct entry);https://stackoverflow.com/questions/42602309
复制相似问题