#include <stdio.h>
#include <stdlib.h>
struct Point {
double x;
};
void test(struct Point **a, int len)
{
int i;
printf("a = %p\n", a);
for (i = 0; i < len; ++i)
printf("%f\n", a[i]->x);
}
int main()
{
int i;
int len = 4;
struct Point *P;
P = malloc(len*sizeof(struct Point));
for (i = 0; i < len; ++i) {
P[i].x = i;
printf("%f\n", P[i].x);
}
printf("&P = %p\n", &P);
test(&P, len);
return 0;
}
我试图将结构数组传递给函数(我想传递指向数组的指针,而不是复制)。当我尝试在函数内使用数组时,出现访问冲突。这样做的正确方法是什么?我究竟做错了什么?a == &P
,所以它应该有效,对吧?
为什么你想要一个struct Point **
?您可以重写为相同的
void test(struct Point *a, int len)
{
//some stuff
printf("%f\n", a[i].x);
}
并称之为
test(P, len);
这样,恕我直言,要求
我想传递一个指向数组的指针
is also met#.
(#) NOTE: To be strict, here we pass the pointer to the first element of the array, however, the behaviour compares equal. Thanks to Mr. @alk for the comment.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)