首页 文章

空结构typedef

提问于
浏览
0

要开始这是一个功课问题:

该项目的目标是为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文件中尝试了几种结构变体,但我想我错过了一些非常明显的东西......

我应该在我的.c文件中使用我的DlList_T结构,将其重命名为其他内容,然后只要我需要它就投出它......?

另外请注意,我不允许以任何方式更改.h文件 . 当我提交项目时,try将使用.h文件的本地副本 .

我很失落,任何帮助将不胜感激,谢谢!

编辑:包含#ifndef和#endif的头文件

编辑2:这是使用-std = c99标志用gcc编译的 .

5 回答

  • 0

    你从这个陈述开始

    typedef struct { } * DlList_T;
    

    其中 typedef 和空结构为 DlList_T 的标识符 .

    然后,这会出现

    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;
    

    这又是 typedef 的相同标识符, DlList_T . 这是非法的 .

  • 0

    我建议在头文件中包含列表节点的结构定义,但是你不能修改它,你必须将它放在 .c 源文件中 .

    您的错误表明您对 DlList_T 类型有多个定义:您不能说 DlList_T 是空结构的类型别名(您在 Headers 中执行)并且在同一类型中指向 struct _DlList_T 的指针(您可以在源文件中执行) .

    因此,第一步:首先为列表变量选择另一个名称,例如 dbl_list

    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
    
    
    } *dbl_list;
    

    定义具有前导 _ 的结构也是一个坏主意,因为这些是为实现保留的 - 考虑选择另一个名称 .

    头文件应该有这个:

    typedef struct DlList *DlList_T;
    

    或类似的东西(可能有不同的结构标签) . 您显示的代码不是标准C(空结构不是标准的),它没有任何意义 . 如果这是您的指示,我认为您应该将其报告为错误 . 或者,确保完成课程练习 - 也许您应该使用具有启用此类构造的扩展的特定编译器 . 但要预先警告它不便携 .

  • 0

    根据您的最新更新,请考虑按以下方式进行操作:

    ...
    
    #define _DLL_IMPL_
    // This is to prevent dlList.h from making that illegal thing
    
    typedef struct _dlNode {
    
        struct _dlNode *prev;
        struct _dlNode *next;
        void * data; //pointer to a memory address
    
    } dlNode;
    
    typedef 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;
    
    #include "dlList.h"
    
    ...
    

    或类似的规定 . 在 .h.c 文件中,确保 DlList_T 在使用之前已被类型定义为有效结构 . 由于您不能违反 .h 文件,因此您应该在包含它之前立即执行此操作 .

    看来你最有可能被要求通过使用适当的预处理器指令来防止这样的非法类型定义,在这种情况下是 #define _DLL_IMPL_

  • 0
    #include <dlList.h>
    
    typedef struct _dlNode
       {
       struct _dlNode *prev;
       struct _dlNode *next;
       void * data; //pointer to a memory address
       } dlNode;
    
    struct _MyList_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
       } * MyList_T;
    
    void dll_clear(DlList_T lst)
       {
       MyList_T list = (MyList_T)lst;
    
       list->cursor...
    
       return;
       }
    
  • 1

    您的第一个错误是 DlList_T 是.h文件中的类型和.c文件中的变量 . 这不行 .

    第二,你不应该

    typedef struct { } * DlList_T;
    

    在.h文件中但是

    typedef struct DlList * DlList_T;
    

    或类似的东西 . 这称为前向声明(两个不同的名称, struct DListDlist_T

    然后在.c文件中使用 {}struct Dlist 声明 . 这就是隐藏C类型工作细节的方法 . 如果你的教师确实在头文件中给你一个带有 {} 的版本,那么他或她应该在教导他人之前先修改基本的C. (空 struct 是C中的约束违规,但可能被gcc接受为扩展名 . )

相关问题