简体   繁体   中英

How to pass a structure element structure to function by pointer

I am trying to change my C functions of nested structures to operate on pointers instead of passing and making copies of the structures which are quite large in reality.

here is a simplified version of what I want to do passing the structures around....

    struct InnerStruct
{
    int int1;
    int int2;
};

struct OuterStruct
{
    struct innerStruct inner1;
    int outerResult;
};

void main (void)
{
    struct OuterStruct outer1;
    outer1 = get_outer ();
}

struct OuterStruct get_outer (void)
{
    struct OuterStruct thisOuter;
    thisOuter.inner1 = get_inner (void);
    thisOuter.outerResult = get_result (thisOuter.inner1);
    return thisOuter;
}

struct InnerStruct get_inner (void)
{
    struct InnerStruct thisInnner;
    thisInner.int1 = 1;
    thisInner.int2 = 2;
    return thisInner;
}

int get_result (struct InnerStruct thisInner)
{
    int thisResult;
    thisResult = thisInner.int1 + thisInner.int2;
    return thisResult;
}

but the structure is quite large in reality and this is a frequent operation, so I'd rather pass around the pointers. Just not sure how the syntax works for nested structures like this. Here is my attempt....

    struct InnerStruct
{
    int int1;
    int int2;
};

struct OuterStruct
{
    struct innerStruct inner1;
    int outerResult;
};

void main (void)
{
    struct OuterStruct outer1;
    get_outer (&outer1);
}

void get_outer (struct OuterStruct *thisOuter)
{
    get_inner (&(thisOuter->inner1));
    thisOuter->outerResult = get_result (&(thisOuter->inner1));
}

void get_inner (struct InnerStruct *thisInner)
{
    thisInner->int1 = 1;
    thisInner->int2 = 2;
}

int get_result (struct OuterStruct *thisInner)
{
    int thisResult;
    thisResult = thisInner->int1 + thisInner->int2;
    return thisResult;
}

This will illustrate an easy way to pass pointers to structs. It is a much more efficient way to pass data around, especially when, as you say, the data can get very large. This illustration uses a compound struct, (struct within struct) with an array and pointer declared to pass around. Comments in code explain things.

This will all build and run so you can experiment with it. ie, follow the data along with execution.

Here is an easy way: (using my own structs)

typedef struct {
    int alfha;
    int beta;
} FIRST;

typedef struct {
    char str1[10];
    char str2[10];
    FIRST first;
}SECOND;               //creates a compound struct (struct within a struct, similar to your example)

SECOND second[5], *pSecond;//create an array of SECOND, and a SECOND * 

SECOND * func(SECOND *a); //simple func() defined to illustrate struct pointer arguments and returns

int main(void)
{
    pSecond = &second[0];  //initialize pSecond to point to first position of second[] (having fun now)
    SECOND s[10], *pS;     //local copy of SECOND to receive results from func
    pS = &s[0];//just like above;

    //At this point, you can pass pSecond as a pointer to struct (SECOND *)
    strcpy(pSecond[0].str2, "hello");
    pS = func(pSecond);

   // printf("...", pS[0]...);//pseudo code - print contents of pS, which now contains any work done in func 

    return 0;   
}

SECOND * func(SECOND *a) //inputs and outputs SECOND * (for illustration, i.e., the argument contains all 
{                        //information itself, not really necessary to return it also)
    strcpy(a[0].str1, "a string");
    return a;
}

Although there is not much going on in func(), when the pointer returns to main(), it contains both the value copied in main, and the value copied in fucn(), as shown here:
Results: (in code) 在此处输入图片说明
Contents in pSecond:

在此处输入图片说明

You should really look up more about how pointers work. But here is some sample C++ code. Notice the "&" tells your compiler to "not send the struct itself" to the function but a pointer to it. Just a warning never return a reference to a variable (unless you know what you are doing).

    #include <iostream>

    struct MyStruct
    {
        int a;
        int b;
    };

    using namespace std;

    void printStruct(MyStruct * mypointer) {
        cout << "MyStruct.a=" << mypointer->a << endl;
        cout << "MyStruct.b=" << mypointer->b << endl;
    }

    int main()
    {
       MyStruct s;
       s.a = 2;
       s.b = 1;

       printStruct(&s);

       return 0;
    }

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