0

I have a header file list.h and a source file list.c,which define the functions in list.h. I have a struct here:

    typedef struct ListNode{
 struct ListNode* next;
 struct ListNode* prev;
 void *value;
    }Node;
    typedef struct List{
 Node *first;
 Node *last;
 int count;
    }List;

How can i make them only visible to the functions in list.h,when the compiler does not accept using static and typedef together? These are the functions i declare in list.h:

    List *List_create();
    void List_destroy(List *list);
    void *List_remove(List *list,Node *node);
Van Teo Le
  • 164
  • 3
  • 11

1 Answers1

0

You can use pointers to opaque structs to hide the contents of your structs List and Node. In the header, use include only the opaque declarations

typedef struct ListNode Node;
typedef struct List List;

and the function declarations. These tell the compiler that the structs and typedefs exist, but not what the structs contain.

In list.c, include the full declarations, but not as typedefs, since the header file already tells the compiler about the typedef'ed names.

struct ListNode {
    /* contents */
};
struct List {
    /* contents */
};

This way the contents of the structs are not usable in other C files, but the functions in list.c can still use them. This also means that other C files cannot create new instances of these structs, except by using the functions offered in list.h.

rtoijala
  • 1,200
  • 10
  • 20