简体   繁体   中英

How to order C structure/function declarations which use each other?

I am trying to create a structure by the name "IExampleVtbl" that will hold a pointer to my functions (SetStringPtr,GetStringPtr) and will be a part of another structure "IExample" .

But I want to pass the other structure "IExample" as parameter to the functions (SetStringPtr,GetStringPtr) .

This is the code:

#include <windows.h>
#include <stdio.h>

typedef struct {
    SetStringPtr *SetString;
    GetStringPtr *GetString;
} IExampleVtbl;

typedef struct {
    IExampleVtbl *lpVtbl;
    DWORD   count;
    char    buffer[80];
} IExample;

typedef long SetStringPtr(IExample *, char *);
typedef long GetStringPtr(IExample *, char *, long);

long SetString(IExample *this, char * str)
{
    ...

    return(0);
}

long GetString(IExample *this, char *buffer, long length)
{
    ...

    return(0);
}

As you can see the first structure need to know about the functions, the functions need to know about the second structure which need to know about the first structure.

How I can solved that?

You can solve the problems by putting things in the following order

  • typedefs for the structures
  • typedefs for the function pointers
  • structure definitions
  • function definitions

To make that work, you'll need to define the structures with tags:

typedef struct IExampleVtblTag IExampleVtbl;
typedef struct IExampleTag IExample;
typedef long SetStringPtr(IExample *, char *);
typedef long GetStringPtr(IExample *, char *, long);

struct IExampleVtblTag {
    SetStringPtr *SetString;
    GetStringPtr *GetString;
};

struct IExampleTag {
    IExampleVtbl *lpVtbl;
    DWORD   count;
    char    buffer[80];
};

long SetString(IExample *this, char * str)
{
    return(0);
}

long GetString(IExample *this, char *buffer, long length)
{
    return(0);
}

You combine a forward declaration with the type-alias definition:

// Forward declaration of the structure IExample
// And at the same time definition of the type-alias IExample
typedef struct IExample IExample;

typedef long SetStringPtr(IExample *, char *);
typedef long GetStringPtr(IExample *, char *, long);

// Now the definition of the structures
typedef struct { ... } IExampleVtbl;

// Because the previous type-alias definition, we need to specify a structure tag
struct IExample { ... };

typedef for the struct to be typedef'd can precede the definition of struct, so a bit of re-arrangement should make things work here

typedef struct IExample IExample;

typedef long SetStringPtr(IExample *, char *);
typedef long GetStringPtr(IExample *, char *, long);

typedef struct {
    SetStringPtr *SetString;
    GetStringPtr *GetString;
} IExampleVtbl;

struct IExample {
    IExampleVtbl *lpVtbl;
    long   count;
    char    buffer[80];
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM