在.h文件中声明一个结构并在.c文件中实现

maa*_*yan 4 c

这是我的.h文件:

struct _MyString;  
typedef struct _MyString MyString;
Run Code Online (Sandbox Code Playgroud)

我想在.c文件中声明其成员.

我试过了:

typedef struct MyString{
    char * _str;// pointer to the matrix
    unsigned long _strLength;
    }MyString;
Run Code Online (Sandbox Code Playgroud)

但它不起作用.我如何在.c文件中声明结构的成员?

谢谢

pmg*_*pmg 12

您只需要1个typedef.保留.h文件中已有的文件并删除所有其他文件.

现在,结构名称是struct _MyString.这是你应该在.c文件中定义的,而不是struct MyString:注意缺少'_'.

所以

.h文件

struct _MyString;  
typedef struct _MyString MyString;
Run Code Online (Sandbox Code Playgroud)

.c文件

#include "file.h"
struct _MyString {
    char * _str;// pointer to the matrix
    unsigned long _strLength;
};
Run Code Online (Sandbox Code Playgroud)