在哪里声明结构
Where to declare structures
我制作了 3 个文件来组织我的代码。一个 main.c、一个 functions.h 和一个 functions.c。我还制作了一个用于某些函数的结构,但我遇到的问题是我找不到任何方法来声明使用 .h 文件中的结构的函数。它工作的唯一方法是当我将函数与结构放在 .c 中而不在 .h 中声明它们中的任何一个时。
我试过了
主要是:
#include "functions.c"
并在 .c:
#include "functions.h"
void myfunction(sinWave *sw, float u, float ts){
};
typedef struct{
float amplitude;
float fase;
} sinWave;
在.h中:
typedef struct sinWave;
void myfunction(sinWave *, float, float);
但是不行。我得到:
warning: useless storage class specifier in empty declaration
'typedef struct sinWave;'a
unknown type name 'sinWave'
'void ePLL(sinWave , float , float);'
不包含.c
,是很糟糕的风格,让它在header
中可见
在functions.h
中:
typedef struct sinWave sinWave;
void myfunction(sinWave *, float, float);
在functions.c
中:
#include "functions.h"
struct sinWave {
float amplitude;
float fase;
};
void myfunction(sinWave *sw, float u, float ts) {
}
在main.c
中:
#include "functions.h"
这样更好:
main.c
:
#include "functions.h"
int main() {
// ...
}
functions.h
:
typedef struct {
float amplitude;
float fase;
} sinWave;
void myfunction(sinWave *, float, float);
functions.c
:
#include "functions.h"
void myfunction(sinWave *sw, float u, float ts) {
// ...
};
这样构建(假设您将使用 sin
、cos
等):
gcc -o xyz main.c functions.c -lm
按照正常方式进行:
// functions.h
// type declaration
typedef struct { . . . } sinWave;
// function prototype
void myfunction(sinWave *, float, float);
和
// functions.c
#include "functions.h"
void myfunction(sinWave *sw, float u, float ts) {
// function definition here
}
typedef struct sinWave;
没有声明类型。它将 sinWave
作为结构 tag 引入。标签本身不是类型名称;它使 struct sinWave
成为一种类型,而不是 sinWave
本身。 (在 C++ 中,它会使 sinWave
成为一种类型。)
要解决此问题,您需要进行两项更改。在 functions.h 中,将 sinWave
声明为 struct sinWave
:
的别名类型
typedef struct sinWave sinWave;
在functions.c中插入结构定义,标签为:
struct sinWave
{
float amplitude;
float fase;
};
(您可能还想正确拼写 phase
。)
请注意,您在 function.c:
中已有结构定义
typedef struct{
float amplitude;
float fase;
} sinWave;
这有两个问题。一个是在一个翻译单元中用标签定义的结构与在另一个翻译单元中没有标签定义的结构不兼容。并且您需要使用标签来识别 typedef
为哪个结构命名(或者您可以在 function.h 中使用 typedef
提供结构的完整定义,但是由于其他原因,这是一个不受欢迎的解决方案。
另一个问题是这个定义出现在myfunction
之后,但是myfunction
可能需要结构体的定义,所以定义必须出现在myfunction
之前。
]
我制作了 3 个文件来组织我的代码。一个 main.c、一个 functions.h 和一个 functions.c。我还制作了一个用于某些函数的结构,但我遇到的问题是我找不到任何方法来声明使用 .h 文件中的结构的函数。它工作的唯一方法是当我将函数与结构放在 .c 中而不在 .h 中声明它们中的任何一个时。
我试过了
主要是:
#include "functions.c"
并在 .c:
#include "functions.h"
void myfunction(sinWave *sw, float u, float ts){
};
typedef struct{
float amplitude;
float fase;
} sinWave;
在.h中:
typedef struct sinWave;
void myfunction(sinWave *, float, float);
但是不行。我得到:
warning: useless storage class specifier in empty declaration
'typedef struct sinWave;'a
unknown type name 'sinWave'
'void ePLL(sinWave , float , float);'
不包含.c
,是很糟糕的风格,让它在header
在functions.h
中:
typedef struct sinWave sinWave;
void myfunction(sinWave *, float, float);
在functions.c
中:
#include "functions.h"
struct sinWave {
float amplitude;
float fase;
};
void myfunction(sinWave *sw, float u, float ts) {
}
在main.c
中:
#include "functions.h"
这样更好:
main.c
:
#include "functions.h"
int main() {
// ...
}
functions.h
:
typedef struct {
float amplitude;
float fase;
} sinWave;
void myfunction(sinWave *, float, float);
functions.c
:
#include "functions.h"
void myfunction(sinWave *sw, float u, float ts) {
// ...
};
这样构建(假设您将使用 sin
、cos
等):
gcc -o xyz main.c functions.c -lm
按照正常方式进行:
// functions.h
// type declaration
typedef struct { . . . } sinWave;
// function prototype
void myfunction(sinWave *, float, float);
和
// functions.c
#include "functions.h"
void myfunction(sinWave *sw, float u, float ts) {
// function definition here
}
typedef struct sinWave;
没有声明类型。它将 sinWave
作为结构 tag 引入。标签本身不是类型名称;它使 struct sinWave
成为一种类型,而不是 sinWave
本身。 (在 C++ 中,它会使 sinWave
成为一种类型。)
要解决此问题,您需要进行两项更改。在 functions.h 中,将 sinWave
声明为 struct sinWave
:
typedef struct sinWave sinWave;
在functions.c中插入结构定义,标签为:
struct sinWave
{
float amplitude;
float fase;
};
(您可能还想正确拼写 phase
。)
请注意,您在 function.c:
中已有结构定义typedef struct{
float amplitude;
float fase;
} sinWave;
这有两个问题。一个是在一个翻译单元中用标签定义的结构与在另一个翻译单元中没有标签定义的结构不兼容。并且您需要使用标签来识别 typedef
为哪个结构命名(或者您可以在 function.h 中使用 typedef
提供结构的完整定义,但是由于其他原因,这是一个不受欢迎的解决方案。
另一个问题是这个定义出现在myfunction
之后,但是myfunction
可能需要结构体的定义,所以定义必须出现在myfunction
之前。