【问题标题】:pointer and reference question (linked lists)指针和参考问题(链表)
【发布时间】:2010-04-24 00:49:05
【问题描述】:

我有以下代码

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 是什么意思。

【问题讨论】:

    标签: c++ pointers reference linked-list


    【解决方案1】:

    Node * &fooreferenceNode *

    所以当你调用它时

    AddNode(A, 123, 99.87);
    

    它会改变 A。

    【讨论】:

    • 好吧,从顶部开始,A 是一个指向结构的指针,在这个函数中,参数是一个指针,它是指针的引用地址?
    • 不,listpointer 只是对A 的引用。请参阅我的答案中的链接。即,当您更改listpointer 时,您实际上是在更改A。这是一个别名。
    • 哦,我明白了,所以'*'实际上在这里充当了指针。不是尊重运算符
    猜你喜欢
    • 1970-01-01
    • 2011-05-16
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多