2011-04-06 97 views
1

我試圖調用功能,但我得到的錯誤:地址作爲參數

warning: passing argument 1 of 'drawPlot' from incompatible pointer type

//call 
    drawPlot(&listData); 


//header 
    void drawPlot(NSMutableArray*); 

的ListData是當然的NSMutableArray

+2

你確定它不是'NSMutableArray *'? – rlc 2011-04-06 19:25:25

+0

當然是我的錯,應該是listData沒有&符號。 – juantorena 2011-04-06 19:26:33

回答

2

取出&。你的變量已經是一個指針了。我假設你已經聲明如下:NSMutableArray *listData;

因此,使用&正在向指向NSMutableArray的函數傳遞一個「指向指針的指針」。

現在,SDK中有許多地方需要NSError **。這是當你將使用&,e.g:

 
NSError *error = nil; 
... 
[SomeClass doSomethingReturningError:&error]; 
if (error != nil) { 
    //something bad happened 
} 

所以,這裏所不同的是過客,你的情況,listData經過值的對象。變量的值本身不能被修改(但是數組的內容可以)。另一種情況(&error)通過引用傳遞,這意味着可以修改變量的值,即指針本身。這就是爲什麼你可以比較結果,在這種情況下,爲了看看是否出了問題。

一個更簡單的實施例是比較以下的區別:

 
void changeIntByValue(int i) 
{ 
    i++; 
} 

void changeIntByReference(int &i) 
{ 
    i++; 
} 

int x=2; 
changeIntByValue(x); 
NSLog(@"%d", x); // prints 2 
changeIntByReference(&x); 
NSLog(@"%d", x); // prints 3 

通過傳遞值不允許參數進行修改,通過引用而確實。

-1

的ListData的定義應該是這個樣子:

NSMutableAarray listData; //OR 
NSMutableAarray listData[]; 

如果是前者,則將drawPlot調用改爲

drawPlot(&listData[0]) // OR 
drawPlot(listData) 

如果是後者,那麼drawPlot電話更改爲

drawPlot(&listData [ the index of the listData array member you want to plot]); // does it work now?