简体   繁体   English

如何将指针传递给在另一个结构内部声明的结构作为函数参数?

[英]How to pass a pointer to a struct declared inside another struct as a function parameter?

In one of my applications written in CI have a struct declared as a member of another struct: 在我用CI编写的一个应用程序中,有一个结构声明为另一个结构的成员:

struct _test
{
    int varA;
    //...

    struct _small
    {
        int varB;
        //...
    } small;
} test;

Now I want to create a function that access varB above, but I don't want it to access the entire structure test , that is, I don't want to do: 现在,我想创建一个可以访问上面的varB的函数,但是我不希望它访问整个结构的test ,也就是说,我不想这样做:

#include <relevant_header>

void myFunction()
{
    test.small.varB = 0;
}

instead, I want to pass only the small structure as a parameter to that function; 相反,我只想将small结构作为参数传递给该函数; something like this: 像这样的东西:

#include <relevant_header>

void myFunction(struct _test::_small* poSmall)
{
    poSmall->varB = 0;
}

The problem is I don't know how to do this, that is, the above code doesn't compile right (I suppose it's C++ syntax only). 问题是我不知道该怎么做,也就是说,上面的代码没有正确编译(我想这只是C ++语法)。 So how may I do this in a C code - pass a pointer to a struct that was declared inside another struct? 那么,如何在C代码中执行此操作-将指针传递给在另一个结构内部声明的结构? I wasn't able to find anything about this both in SO as well as in Google in general. 在SO和Google中,我都找不到关于此的任何信息。

Just do: 做就是了:

void myFunction(struct _small *poSmall)
{
    poSmall->varB = 0;
}

The scope of struct _small is not limited to its outer structure. struct _small的范围不限于其外部结构。

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

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