2010-11-30 18 views
24

このコードは正しいですか?それは期待どおりに動作しますが、このコードはポインタとドット表記法を使用して構造体に正しく使用されていますか?構造体をC言語で参照渡しします

struct someStruct { 
unsigned int total; 
}; 

int test(struct someStruct* state) { 
state->total = 4; 
} 

int main() { 
struct someStruct s; 
s.total = 5; 
test(&s); 
printf("\ns.total = %d\n", s.total); 
} 
+1

なぜintが返されるのでしょうか?実際にそこに戻ることはありません。 – Muggen

+0

Cには参考文献がありますか? Suprised ... –

+1

@honk:C++の意味ではなく、C++が発明される前は、ポインタは一種の「参照」でした(「referand」があるため)。今では彼らは "尖っている"またはそのようなナンセンスを持っています。 'someStruct'は値のセマンティクスを持っているので、" C++の言葉ではCがしない "" pass-by-reference "は、" value by reference "を渡すこととまったく同じです(これは問題の" reference "ポインタである)。 –

答えて

15

これは構造体の正しい使い方です。あなたの戻り値について質問があります。

また、符号なし整数をprintfingするので、%dの代わりに%uを使用する必要があります。

1

イエップ。あたりです。もしそれが(/ - >観点から)なければ、あなたのコンパイラは叫ぶでしょう。

3

はい、そうです。構造体sを作成し、その合計を5に設定し、ポインタを使用して合計を4に設定した関数にポインタを渡し、それを出力します。 ->は構造体へのポインタのメンバー用であり、.は構造体のメンバー用です。あなたがそれらを使ったように。

ただし、戻り値は異なります。 testはおそらく無効になり、mainの最後にreturn 0が必要です。

39

あなたのポインタとドット表記の使用は良いです。コンパイラは、問題があった場合、エラーや警告を出すべきです。

ここでは、構造とポインタと関数と変数のスコープの使用について考えるまで、いくつかの追加の注釈と注意事項を記載したコードのコピーです。

// Define the new variable type which is a struct. 
// This definition must be visible to any function that is accessing the 
// members of a variable of this type. 
struct someStruct { 
    unsigned int total; 
}; 

/* 
* Modifies the struct that exists in the calling function. 
* Function test() takes a pointer to a struct someStruct variable 
* so that any modifications to the variable made in the function test() 
* will be to the variable pointed to. 
* A pointer contains the address of a variable and is not the variable iteself. 
* This allows the function test() to modify the variable provided by the 
* caller of test() rather than a local copy. 
*/ 
int test(struct someStruct *state) { 
    state->total = 4; 
    return 0; 
} 

/* 
* Modifies the local copy of the struct, the original 
* in the calling function is not modified. 
* The C compiler will make a copy of the variable provided by the 
* caller of function test2() and so any changes that test2() makes 
* to the argument will be discarded since test2() is working with a 
* copy of the caller's variable and not the actual variable. 
*/ 
int test2(struct someStruct state) { 
    state.total = 8; 
    return 0; 
} 

int test3(struct someStruct *state) { 
    struct someStruct stateCopy; 
    stateCopy = *state; // make a local copy of the struct 
    stateCopy.total = 12; // modify the local copy of the struct 
    *state = stateCopy; /* assign the local copy back to the original in the 
           calling function. Assigning by dereferencing pointer. */ 
    return 0; 
} 

int main() { 
    struct someStruct s; 

    /* Set the value then call a function that will change the value. */ 
    s.total = 5; 
    test(&s); 
    printf("after test(): s.total = %d\n", s.total); 

    /* 
    * Set the value then call a function that will change its local copy 
    * but not this one. 
    */ 
    s.total = 5; 
    test2(s); 
    printf("after test2(): s.total = %d\n", s.total); 

    /* 
    * Call a function that will make a copy, change the copy, 
     then put the copy into this one. 
    */ 
    test3(&s); 
    printf("after test3(): s.total = %d\n", s.total); 

    return 0; 
} 
+0

変数の名前の前に "\ *"を付けることの意義(この投稿のように)またはタイプ名の後に(OPのように)何が重要なのでしょうか? HERE : int型の試験(構造体someStruct \ *状態) OP:変数の型を指定するときにアスタリスクが示す int型の試験(構造体someStruct \ *状態) – ALM

+0

@ALMは、それはそれへのポインタであることを示し、変数の型。だから 'someStruct state'は' someStruct'型の変数ですが、 'someStruct * state'は' someStruct'型の変数へのポインタです。 [Pointer Basics](http://cslibrary.stanford.edu/106/)を参照してください。 –

+0

あなたの応答を感謝します。しかし、私の質問は*のためのものではなかった、私の質問は、構造体のtypenameの後に*(a)を持つことの重要性と構造体の割り当てられた名前の前にそれを持つことでした。私はそれが私のquestinをより明確にしたことを願っている。 – ALM

関連する問題