【发布时间】:2010-04-27 00:29:48
【问题描述】:
我在将结构数组作为函数的参数传递时遇到问题
struct Estructure{
int a;
int b;
};
还有一个函数
Begining(Estructure &s1[])
{
//modifi the estructure s1
};
主要是这样的
int main()
{
Estructure m[200];
Begining(m);
};
这有效吗?
【问题讨论】:
我在将结构数组作为函数的参数传递时遇到问题
struct Estructure{
int a;
int b;
};
还有一个函数
Begining(Estructure &s1[])
{
//modifi the estructure s1
};
主要是这样的
int main()
{
Estructure m[200];
Begining(m);
};
这有效吗?
【问题讨论】:
不,你需要 typedef 你的结构,你应该将数组传递给它;通过引用传递在 C 中不起作用。
typedef struct Estructure{
int a;
int b;
} Estructure_s;
Begining(Estructure_s s1[])
{
//modify the estructure s1
}
int main()
{
Estructure_s m[200];
Begining(m);
}
或者:
struct Estructure{
int a;
int b;
};
Begining(struct Estructure *s1)
{
//modify the estructure s1
}
int main()
{
struct Estructure m[200];
Begining(m);
}
【讨论】:
Begining(struct Estructure s1[])
{
//modifi the estructure s1
};
int main()
{
struct Estructure m[200];
Begining(m);
return 0;
};
【讨论】:
typedef struct{
int a;
int b;
} Estructure;
void Begining(Estructure s1[], int length)
//Begining(Estructure *s1) //both are same
{
//modify the estructure s1
};
int main()
{
Estructure m[200];
Begining(m, 200);
return 0;
};
注意:最好将length 添加到您的函数Beginning。
【讨论】:
要么将工作结构粘贴在 Estructure 前面,要么将其 typedef 到其他类型并使用它。在 C 中也不存在按引用传递,如果您愿意,可以传递一个指针。也许:
void Begining(struct Estructure **s1)
{
s1[1]->a = 0;
}
与数组不太一样,但这应该在 C 语言环境中工作,并且它传递一个指针以提高效率。
【讨论】:
typedef struct { int a; int b;} 结构;
void Begining(Estructure *svector) { 向量[0].a =1; }
【讨论】: