简体   繁体   English

如何订购彼此使用的C结构/函数声明?

[英]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" . 我试图创建一个名为“IExampleVtbl”的结构,它将保存指向我的函数的指针(SetStringPtr,GetStringPtr) ,并将成为另一个结构“IExample”的一部分

But I want to pass the other structure "IExample" as parameter to the functions (SetStringPtr,GetStringPtr) . 但我想将其他结构“IExample”作为参数传递给函数(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 结构的typedef
  • typedefs for the function pointers 函数指针的typedef
  • 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: 您将前向声明与type-alias定义结合使用:

// 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的结构的typedef可以在struct的定义之前,所以稍微重新安排应该让事情在这里工作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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