【问题标题】:How to pass a structure element structure to function by pointer如何通过指针将结构元素结构传递给函数
【发布时间】:2013-11-15 18:27:21
【问题描述】:

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

【问题讨论】:

  • 乍一看,这似乎是正确的,除了在OuterStruct 的定义中应该拼写为InnerStructinnerStruct。究竟是什么问题?
  • 真的是我写了这个简化的例子来理解我想要做什么。实际的代码又长又复杂,很容易迷失在其中。我发布这篇文章的主要原因是我希望人们会跳出来说不要那样做,你做错了!所以感谢您查看它,我知道我本可以继续尝试编译它,但正在获得最佳实践建议。
  • 真的,我没有找到让我失望的东西(因为玩过它后我意识到没有必要)但是假设我希望 get_result 函数在外部指针...我不确定说“thisResult = thisOuter->inner1->int1 + thisOuter->inner1->int2;”是否有意义但是阅读了解除引用快捷方式的工作原理后,我意识到在这种情况下我应该说'thisResult = thisOuter->inner1.int1 + thisOuter->inner1.int2;'

标签: c function pointers struct arguments


【解决方案1】:

您真的应该更多地了解指针的工作原理。但这里有一些示例 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;
    }

【讨论】:

    【解决方案2】:

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

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

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

    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()中复制的值,如下所示:
    结果:(在代码中)
    pSecond 中的内容:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      • 2021-11-02
      相关资源
      最近更新 更多