我目前正在处理一个分配,其中创建了一个ListNode结构,该结构包含指向链表中下一个ListNode的指针,以及一个指向存储有关该节点的信息的Info结构的指针。
我目前有以下代码:
info.h
#ifndef info
#define info
//Define the node structure
typedef struct Info {
size_t pos;
size_t size;
} Info_t;
#endif
listNode.h
#ifndef listNode
#define listNode
//Define the node structure
typedef struct ListNode {
struct Info_t *info;
struct ListNode *next;
} ListNode_t;
ListNode_t* newListNode(ListNode_t* next, Info_t* info);
void destroyListNode(ListNode_t* node);
#endif
listNode.c
#include <stdio.h>
#include <stdlib.h>
#include "info.h"
#include "listNode.h"
ListNode_t* newListNode(ListNode_t* next, Info_t* info)
{
//Set the current node to the head of the linked list
ListNode_t *current = next;
//Move to the next node as long as there is one. We will eventually get to the end of the list
while (current->next != NULL) {
current = current->next;
}
//Create a new node and initialise the values
current->next = malloc(sizeof(ListNode_t));
current->next->info = info;
return current;
}
void destroyListNode(ListNode_t* node)
{
}
当我尝试对此进行编译时,出现以下错误,并且终生无法弄清这是哪里出了错。
gcc -g -Wall listNode.c -o listNode
In file included from listNode.c:7:0:
listNode.h:9:24: error: expected identifier or ‘(’ before ‘;’ token
struct Info_t *info;
^
listNode.c: In function ‘newListNode’:
listNode.c:9:1: error: parameter name omitted
ListNode_t* newListNode(ListNode_t* next, Info_t* info)
^~~~~~~~~~
listNode.c:21:25: error: expected identifier before ‘=’ token
current->next->info = info;
^
任何帮助将不胜感激。
答案 0 :(得分:8)
包含保护的名称与您在程序中使用的标识符冲突。您在此处定义info
:
#define info
具体来说,您将其定义为空。所以这里
current->next->info = info;
变成这样:
current->next->= ;
然后struct Info_t *info;
变成struct Info_t *;
。那些显然不会编译。您需要将包含保护中的info
重命名为不会与其他内容冲突的内容,例如INFO_H_GUARD
。