简体   繁体   中英

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:

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:

#include <relevant_header>

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

instead, I want to pass only the small structure as a parameter to that function; 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). So how may I do this in a C code - pass a pointer to a struct that was declared inside another struct? I wasn't able to find anything about this both in SO as well as in Google in general.

Just do:

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

The scope of struct _small is not limited to its outer structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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