私は、データを格納する構造体と構造体の配列を作成し、私の機能にそれらのそれぞれを通過したC.にかなり新しいです:C:関数内の構造体の内部構造体の配列へのポインタ
int dosomething(struct s1 *struct1, struct s2 *struct2);
struct S1 {
int a;
int b;
int c;
};
struct S2 {
double x;
double y;
double z;
};
int main()
{
int n = 200;
struct S1 s1;
struct S2 *s2 = malloc(n * sizeof(struct S2));
dosomething(&s1, &s2[0])
return 0;
}
int dosomething(struct S1 *s1, struct S2 *s2)
{
s1->a = 1;
s1->b = 2;
s1->c = 3;
s2[0].x = 1.1;
s2[1].x = 1.123233;
...
return 1;
}
これは時間の経過とともにかなり迷惑になりました。 私は構造体に、アレイへの、構造体の配列へのポインタについて多くを読んでというように、私はまだわからない、どのようなI場合: は、今私は今、事はある。この
struct S1 {
int a;
int b;
int c;
struct S2 *s2;
};
struct S2 {
double x;
double y;
double z;
};
int main()
{
int n = 200;
struct S1 s1;
struct S2 *s2 = malloc(n * sizeof(struct S2));
s1.s2 = s2;
dosomething(&s1)
return 0;
}
int dosomething(struct S1 *s1)
{
s1->a = 1;
s1->b = 2;
s1->c = 3;
s1->s2[0].x = 1.1;
s1->s2[1].x = 1.123233;
...
// OR
struct S2 *s2 = s1->s2;
s2[0].x = 1.1;
s2[1].x = 1.123233;
...
}
をしていますここにいるのは正しいです。 , c pointer to array of structs, How to declare pointer to array of structs in C, Pointer to array of struct in function。 私のプロジェクトは、それをより良くしようとすることによってそれを悪化させたくない大きさに成長しています。
s2.s1 [x]へのポインタの配列を作成する必要はありますか?
1つのポインタ(struct S2 * s2 = s1-> s2)にs2の要素数を伝える必要がありますか?
落とし穴がありますか、または何かが間違っている可能性がありますか?
コンパイラに警告が表示されますか? 'main()'の暗黙の宣言について –
あなたがしたことは言語の観点からは問題ないようです(エラーはなく、正しい機能を持っています)。しかし、それはもっとうまくできます。 –
'n'はどこに定義されていますか? – Groo