【发布时间】:2015-05-18 17:33:31
【问题描述】:
在我用 C 编写的一个应用程序中,我有一个结构声明为另一个结构的成员:
struct _test
{
int varA;
//...
struct _small
{
int varB;
//...
} small;
} test;
现在我想创建一个访问上面varB的函数,但是我不希望它访问整个结构test,也就是我不想这样做:
#include <relevant_header>
void myFunction()
{
test.small.varB = 0;
}
相反,我只想将small 结构作为参数传递给该函数;像这样:
#include <relevant_header>
void myFunction(struct _test::_small* poSmall)
{
poSmall->varB = 0;
}
问题是我不知道该怎么做,也就是说,上面的代码编译不正确(我想它只是C++语法)。那么如何在 C 代码中执行此操作 - 将指针传递给在另一个结构中声明的结构?我在 SO 和 Google 中都找不到任何关于此的内容。
【问题讨论】:
-
::是 C 中的一个语法错误。只需使用struct _small来使用struct _small类型的变量。在 C 中没有嵌套结构的范围,就像在 C++ 中一样。并注意使用以下划线开头的名称。
标签: c function pointers struct parameters