[英]C- Passing and Deferencing Structure Arrays
I have this C code for a Point structure: 我有一个Point结构的C代码:
typedef struct Point{
int x, y;
} Point;
void one(Point P) {
p.x = 1;
p.y = 1;
}
I would like to make a pointer to an array of 50 points and then pass these some of the array members to the one function. 我想制作一个指向50个点的数组的指针,然后将这些数组成员传递给一个函数。
I tried 我试过了
struct Point (*mypointer)[50];
one(mypointer[i]);
but got "expected 'Point' but argument is of type 'struct Point'" I then tried deferencing the pointer 但是得到了“预期的'Point'但是参数类型为'struct Point'”然后我尝试引用指针
one(&mypointer[i])
but got "expected 'Point' but argument is of type 'struct Point (*)[(long unsigned int)N]" What should I do? 但得到了“预期的'Point',但参数的类型是'struct Point(*)[(long unsigned int)N]”我该怎么办? Thanks.
谢谢。
That's because argument for function one
is not a reference to a structure but just real data (which is allocated on the stack). 这是因为函数
one
的参数不是对结构的引用,而是实际数据(在堆栈上分配)。
Try by using: 尝试使用:
void one(Point *p) {
p->x = 1;
p->y = 1;
}
In any case the array declared as struct Point *mypointer[50]
is not an array to points but an array of pointers to points (which you have to allocate one by one). 在任何情况下,声明为
struct Point *mypointer[50]
的数组都不是指向点的数组,而是指向点的指针数组(必须逐个分配)。 If you wish to have just an array to points you should use 如果你希望只有一个数组来表示你应该使用的点数
Point points[50];
one(&points[i])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.