2010-04-24 63 views
0

我有以下代碼指針和引用的問題(鏈表)

struct Node { 
    int accnumber; 
    float balance; 
    Node *next; 
}; 

Node *A, *B; 

int main() { 
    A = NULL; 
    B = NULL; 
    AddNode(A, 123, 99.87); 
    AddNode(B, 789, 52.64); 
    etc… 
} 

void AddNode(Node * & listpointer, int a, float b) { 
// add a new node to the FRONT of the list 
Node *temp; 
    temp = new Node; 
    temp->accnumber = a; 
    temp->balance = b; 
    temp->next = listpointer; 
    listpointer = temp; 
} 
在這所 void AddNode(Node * & listpointer, int a, float b) {

是什麼*& listpointer意味着什麼。

回答

2

Node * &fooreferenceNode *

所以,當你與

AddNode(A, 123, 99.87); 

叫它它會改變A.

+0

確定,所以從頂部,是一個指向結構,而在這個函數中,參數是一個指針,它是指針的引用地址? – silent 2010-04-24 00:56:02

+0

不,'listpointer'只是對'A'的引用。請參閱我答案中的鏈接。也就是說,當你改變'listpointer'時,你確實正在改變'A'。這是一個別名。 – 2010-04-24 00:59:46

+0

哦,我明白了,所以'*'實際上就是指針。不是尊敬的運營商 – silent 2010-04-24 01:03:22