我写了下面的代码,其中包含一个函数,其指针指向大小为3的双精度数组。我的问题是:当我将指针的地址传递给双变量(显然不是数组),然后想要改变函数"f"中这个双变量的值,如下所示,当我以这种方式实现时,结果是正确的,变量的值被改变了:
#include <stdio.h>
void f(double (*)[3]);
double a = 7.5;
int main()
{
double* b = &a;
f(&b);
printf("a = %lfn", a);
return 0;
}
void f(double (*hi)[3])
{
double **sth = (double **) hi;
*(*sth) = 1;
}
但是当我实现如下值不改变:
void f(double (*hi)[3]){
(*hi)[0] = 1;
}
首先修复程序给出的编译错误。解决这些问题后,你就知道问题所在了。
prog.c: In function 'main':
prog.c:8:6: error: passing argument 1 of 'f' from incompatible pointer type [-Werror=incompatible-pointer-types]
f(&b);
^
prog.c:2:6: note: expected 'double (*)[3]' but argument is of type 'double **'
void f(double (*)[3]);
http://ideone.com/mueyCU #include <stdio.h>
void f(double (*)[3]);
double a = 7.5;
int main()
{
double* b = &a;
f(b);
printf("a = %lfn", a);
return 0;
}
void f(double (*hi)[3]){
(*hi)[0] = 1;
}
上面的代码不是正确的做事方式,应该不惜一切代价避免