Domanda

To start this is a homework question:

The goal of this project is to implement a double linked-list for a void * data type.

I am given a .h file with the following struct definition:

//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

and in my dlList.c file I am attempting to do something like this:

//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 

All my errors have to do with variations of

Conflicting types for 'DlList_T'

I've tried several variations of the struct in the .c file, but I think I'm missing something quite obvious...

Should I just take my DlList_T struct in my .c file, rename it to something else and then just cast it whenever I need it...?

As another note I am NOT allowed to change the .h file in any way. When I submit the project a local copy of the .h file will be used by try.

I'm quite lost and any help will be appreciated, thanks!

Edit: Included the #ifndef and #endif's for the header file

Edit 2: This is compiled with gcc using the -std=c99 flag.

È stato utile?

Soluzione 2

#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;
   }

Altri suggerimenti

Your first error is that DlList_T is a type in the .h file and a variable in the .c file. This can't work.

Second, you shouldn't have

typedef struct { } * DlList_T;

in the .h file but

typedef struct DlList * DlList_T;

or something similar. This is called a forward declaration (for two different names, struct DList and Dlist_T)

And then go for the declaration with {} of struct Dlist in your .c file. This is how hiding of the details of a type work in C. If your instructor has really given you a version with the {} in the header file, he or her should first revise basic C before teaching to others. (Empty struct is a constraint violation in C, but maybe accepted as an extension by gcc.)

You start with this statement

typedef struct { } * DlList_T;

Which typedefs and empty struct to the identifier of DlList_T.


Then, later on, this appears

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;

Which again typedefs something to the same identifier, DlList_T. This is illegal.

I would advice to include the structure definition for a list node in the header file, but you can't modify it, you must place it in the .c source file.

Your error indicates that you have more than one definition for the type DlList_T: you can't say that DlList_T is a type alias for an empty struct (which you do in the header) and at the same type a pointer to struct _DlList_T (which you do in the source file).

So, first step: start by choosing another name for your list variable, for example, 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;

It is also a bad idea to define structures with leading _, since these are reserved for the implementation - consider choosing another name.

The header file should have this instead:

typedef struct DlList *DlList_T;

Or something similar (maybe with a different structure tag). The code you show is not standard C (empty structures are not standard), and it makes no sense. If this is what your instruction provided, I think you should report it as a bug. OR, make sure to go through the class exercises - maybe you are supposed to use a specific compiler with extensions that enable this kind of construct. But be forewarned that it is not portable.

Following your latest update, consider making something as follows:

...

#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"

...

Or something along those lines. Make sure that DlList_T has been type-defined to be a valid structure, before getting used, in the .h or the .c files. Since you cannot violate the .h file, you should probably just do it right before including it.

It seems that you are most likely being asked to prevent such an illegal type-definition from being made, by using the appropriate preprocessor directive, which is in this case the #define _DLL_IMPL_

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top