【发布时间】:2014-05-28 21:05:57
【问题描述】:
这是一个家庭作业问题:
该项目的目标是为 void * 数据类型实现双链表。
我得到了一个具有以下结构定义的 .h 文件:
//dlList.h
#ifndef _DLLIST_ADT_H_
#define _DLLIST_ADT_H_
#include <stdbool.h>
#ifndef _DLL_IMPL_
/// DlList_T points to a representation of a double-linked list
/// of void pointers (to abstract data objects).
typedef struct { } * DlList_T;
#endif
//Function declarations below
#endif
在我的 dlList.c 文件中,我正在尝试做这样的事情:
//dlList.c
typedef struct _dlNode{
struct _dlNode *prev;
struct _dlNode *next;
void * data; //pointer to a memory address
} dlNode;
struct _DlList_T{
struct _dlNode *start; //the first item in the list
struct _dlNode *cursor; //the current item the list is pointing to
int curIndex; //index of current item
int maxIndex; //number of items in list
} * DlList_T;
//Rest of .c file
我所有的错误都与
的变体有关“DlList_T”的类型冲突
我已经尝试了 .c 文件中结构的几种变体,但我认为我遗漏了一些非常明显的东西......
我是否应该将我的 DlList_T 结构放入我的 .c 文件中,将其重命名为其他名称,然后在需要时将其转换...?
另外请注意,我不允许以任何方式更改 .h 文件。当我提交项目时,try 将使用 .h 文件的本地副本。
我很迷茫,任何帮助将不胜感激,谢谢!
编辑:包含头文件的#ifndef 和#endif
编辑 2:这是使用 gcc 使用 -std=c99 标志编译的。
【问题讨论】:
-
您是否需要在 .c 文件中定义 DlList_T ? (您是否需要使用 .h 文件中的 DlList_T 结构?)
-
空的
struct在标准 C 中是非法。(某些编译器可能支持也可能不支持它作为扩展。) -
@Mahonri 不确定,但我相信是这样。头文件中的一个示例函数是
void dll_clear( DlList_T lst );,所以我认为它必须有一些声明。我还编辑了我的原始问题以包含 #ifndef 和 #endif 标签。 -
@Keith 我们使用的是标准的 gnu c 编译器,如果这有影响的话。
-
gcc 不严格遵循标准,除非你告诉它,比如
-std=c99 -pedantic。 gcc 恰好支持空结构作为扩展,但我不建议依赖它,除非你有一个 very 很好的理由这样做。该评论谈到“空指针的双链表”;你可能需要在你的结构中有一个void*成员。
标签: c struct void-pointers