简体   繁体   English

如何通过指针将结构元素结构传递给函数

[英]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. 我正在尝试更改嵌套结构的C函数以对指针进行操作,而不是传递和复制实际上相当大的结构的副本。

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: 尽管func()没什么大不了的,但是当指针返回main()时,它既包含在main中复制的值,又包含在fucn()中复制的值,如下所示:
Results: (in code) 结果:(以代码形式) 在此处输入图片说明
Contents in pSecond: pSecond中的内容:

在此处输入图片说明

You should really look up more about how pointers work. 您应该真正更多地了解指针的工作方式。 But here is some sample C++ code. 但是这是一些示例C ++代码。 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;
    }

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

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