1

我正在尝试更改我的嵌套结构的 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;
}
4

2 回答 2

0

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

这将全部构建并运行,因此您可以对其进行试验。即,在执行的同时跟踪数据。

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

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() 中复制的值,如下所示:
结果:(代码中) 在此处输入图像描述
目录在 pSec 中:

在此处输入图像描述

于 2013-11-05T00:01:17.363 回答
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;
    }
于 2013-11-05T00:05:14.617 回答