views:

80

answers:

1

Hi there, I have the following code

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;
}

in this here void AddNode(Node * & listpointer, int a, float b) { what does *& listpointer mean exactly.

+2  A: 

Node * &foo is a reference to a Node *

So when you call it with

AddNode(A, 123, 99.87);

it will change A.

Alex
ok so from the top, A is a pointer to a structure, and in this function the argument is a pointer which is the deferenced address of a the pointer?
sil3nt
No, `listpointer` is just a reference to `A`. See the link in my answer. I.e., when you change `listpointer`, you're really changing `A`. It's an alias.
Alex
oh I see, so the '*' is actually acting as the pointer here. not the deference operator
sil3nt
Correct. [padding]
Alex
thank you very much.
sil3nt