编译预警无效**和无效* [英] Compilation warning for void ** and void *
问题描述
我有一个关于一个问题无效*
和无效**
,我知道这是有点老问题和计算器之前一直问(有点)。所以现在的问题是:
当我编译这个code用gcc 4.4.3 ubuntu下10.10,我得到以下警告:
zz.c:在函数'主':
zz.c:21:警告:从兼容的指针类型过客酒吧的参数1
zz.c:9:注:应为无效**,但参数的类型为浮**
那为什么其确定传递变量x作为的foo()的变量,但它不是确定传递变量y,酒吧的参数()。我可以通过两个变量明确铸造成解决这个问题无效*
和无效**
预期。
无效美孚(无效*一){
}空巴(无效**一){
* A =(浮点*)malloc的(100 * sizeof的(浮动));
}诠释主(){ 浮动* X =(浮点*)malloc的(100 * sizeof的(浮动));
富(X);
免费(X); 浮动* Y;
酒吧(安培; Y);
免费(Y); 返回0;
}
无效* A
意味着 A
点未知类型的对象。然而, A
本身并不是一个未知的类型,因为 A
是已知有键入无效*
。只有 A
点对象有一个未知类型。
无效** A
意味着 A
指向类型的对象无效*
。该 *一个
点,有一个未知的类型,但对象 *一个
本身是有类型的指针<对象code>无效* 。
&安培; Y
是一个指针类型的对象浮法*
。 &安培; Y
不是一个指针类型的对象无效*
I have a question regarding void*
and void**
and I know this is sort of an old question and has been asked (somewhat) before in stackoverflow. So the question is the following:
When I compile this code with gcc 4.4.3 under ubuntu 10.10, I get the following warning:
zz.c: In function ‘main’:
zz.c:21: warning: passing argument 1 of ‘bar’ from incompatible pointer type
zz.c:9: note: expected ‘void **’ but argument is of type ‘float **’
why is it that its ok to pass variable x as an argument of foo() but its not ok to pass variable y as an argument of bar(). I can fix this by explicitly casting both variables into void*
and void**
as expected.
void foo (void* a){
}
void bar(void **a){
*a = (float *) malloc(100*sizeof(float));
}
int main (){
float *x = (float*) malloc(100*sizeof(float));
foo(x);
free(x);
float *y;
bar(&y);
free(y);
return 0;
}
void *a
means that a
points to an object of unknown type. However, a
itself is not an unknown type because a
is known to have type void*
. Only the object that a
points to has an unknown type.
void **a
means that a
points to an object of type void*
. The object that *a
points to has an unknown type, but the object *a
itself is a pointer with type void*
.
&y
is a pointer to an object of type float*
. &y
is not a pointer to an object of type void*
.
这篇关于编译预警无效**和无效*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!