如何通过指针传递结构元素结构

时间:2013-11-04 23:52:15

标签: c function pointers struct arguments

我正在尝试更改嵌套结构的C函数以对指针进行操作,而不是传递和复制实际上非常大的结构。

这是我想要传递结构的简化版本......

    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;
}

但实际上结构非常大,这是一个经常运行,所以我宁愿绕过指针。只是不确定语法如何适用于这样的嵌套结构。这是我的尝试......

    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;
}

2 个答案:

答案 0 :(得分:0)

这将说明一种将指针传递给结构的简单方法。这是一种更有效的传递数据的方法,尤其是当您说,数据可能变得非常大时。此插图使用复合结构(struct中的struct),并声明要传递的数组和指针。代码中的注释解释了事情。

这将全部构建并运行,以便您可以进行实验。即,跟随数据和执行。

这是一个简单的方法:(使用我自己的结构)

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;
}

虽然func()中没有太多内容,但当指针返回main()时,它包含main中复制的值和fucn()中复制的值,如下所示:
结果:(代码中) enter image description here
pSecond中的内容:

enter image description here

答案 1 :(得分:0)

你应该更多地了解指针是如何工作的。但这里有一些示例C ++代码。请注意“&”告诉你的编译器“不发送结构本身”到函数,但指向它。只是一个警告永远不会返回对变量的引用(除非你知道你在做什么)。

    #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;
    }