开发者

How to forward typedef'd struct in .h

开发者 https://www.devze.com 2023-04-01 11:15 出处:网络
I have Preprocessor.h #define MAX_FILES 15 struct Preprocessor { FILE fileVector[MAX_FILES]; int currentFile;

I have Preprocessor.h

#define MAX_FILES 15

struct Preprocessor {
    FILE fileVector[MAX_FILES];
    int currentFile;
};

typedef struct Preprocessor Prepro;

void Prepro_init(Prepro* p) {
    (*p).currentFile = 0;
}

I realized then that I had to separate declarations from definitions. So I created Preprocessor.c:

#define MAX_FILES 15

struct Preprocessor {
    FILE fileVector[MAX_FILES];
    int currentFile;
};

typedef struct Preprocessor Prepro;

And Preprocessor.h is now:

void Prepro_init(Prepro* p) {
    (*p).currentFile = 0;
}

That obviously, doesn't work because Pr..h doesn't know Prepro ty开发者_开发百科pe. I already tried several combinations, none of them worked. I can't find the solution.


Move the typedef struct Preprocessor Prepro; to the header the file and the definition in the c file along with the Prepro_init definition. This is will forward declare it for you with no issues.

Preprocessor.h

#ifndef _PREPROCESSOR_H_
#define _PREPROCESSOR_H_

#define MAX_FILES 15

typedef struct Preprocessor Prepro;

void Prepro_init(Prepro* p);

#endif

Preprocessor.c

#include "Preprocessor.h"

#include <stdio.h>

struct Preprocessor {
    FILE fileVector[MAX_FILES];
    int currentFile;
};

void Prepro_init(Prepro* p) {
    (*p).currentFile = 0;
}


If you want to hide the definition of Preprocessor, you can simply put this in the header file :

struct Preprocessor;
typedef struct Preprocessor Prepro;

But more generally, you'll probably also need the Preprocessor definition in the header file, to allow other code to actually use it.


You have put in .c what should be in .h, and vice versa. Prepro_init must be in .c file, and that file must #include "Preprocessor.h".


YAS:Yet Another Solution.

Preprocessor.h

<some code>
    void Prepro_init(Prepro* p) {
        (*p).currentFile = 0;
    }
<some code>

Preprocessor.c

#define MAX_FILES 15

struct Preprocessor {
    FILE fileVector[MAX_FILES];
    int currentFile;
};
typedef struct Preprocessor Prepro;

#include "Preprocessor.h"      //include after defining your structure.

         <some code> 
        {
              struct Prepro p;
              Prepro_init(p);
     <some code> 
          .... using p.currentFile.....
          .....using other members....
     <some code> 
        }
           <some code> 

Now it will work. I think this is your requirement. Hope it helps.

Drawback: The members of the structure Preprocessor, must be predetermined. i.e the header file uses the member currentFile. So, c file which includes Preprocessor.h must have a structure which is typedefined as Prepro and that structure must include a member currentFile.(in this case).

The same problem I had a year before, while writting a header file to display a users Avl tree in a graphical tree format.


I would suggest that you follow Linus[1], and do not use typedef struct.

If you are in control over the library:

Include file

struct Foo;

int foo_get_n(const struct Foo *bar);

In implementation file

struct Foo
{int n;};

int foo_get_n(const struct Foo *bar)
{return bar->n;}

If you are not in control over the library:

Ask the maintainer to remove these polluting typedefs from the include files.

Summary

Do not use typedef struct.

[1] http://yarchive.net/comp/linux/typedefs.html


Swap .h and .c file. Include header in .c.

Also refer to a book about declarations, definitions and what header files do.

0

精彩评论

暂无评论...
验证码 换一张
取 消