我有一個關於void*
和void**
的問題,我知道這是一個古老的問題,並已(在stackoverflow之前)詢問過(有點)。所以現在的問題是:編譯警告無效**和無效*
當我編譯這個代碼用gcc 4.4.3 ubuntu下10.10,我得到以下警告:
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 **’
那爲什麼其確定傳遞變量x作爲foo()的參數,但不能將變量y作爲bar()的參數傳遞。按照預期,我可以通過明確地將這兩個變量轉換爲void*
和void**
來解決此問題。
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;
}
爲什麼選擇C++標籤?這個問題純粹是c。 –