1

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;
}
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() でコピーされた値の両方が含まれます。
結果: (コード内) ここに画像の説明を入力
内容pSecon で:

ここに画像の説明を入力

于 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 に答える