我正在尝试 C++ 上的链接列表,当我尝试打印列表的数据时,它给出了有趣的结果,告诉我列表为空。当我在 main 中打印变量的地址和在函数中打印同一变量的参数地址时,会给出不同的结果。有谁可以帮我解释一下吗,谢谢!这是代码:
#include <iostream>
using namespace std;
typedef struct link_node{
int data;
link_node* next;
};
void iter(link_node* head){
link_node* cur = head;
cout<<"The address of head in function: "<<&head<<endl;
while(cur!=NULL){
cur = cur->next;
cout<<cur->data<<' ';
}
}
link_node *create(int a){
link_node* head;
link_node* cur;
head =(link_node*) malloc(sizeof(link_node));
cur = head;
for(int i=a;i<a+10;i+=2){
link_node * node = (link_node*) malloc(sizeof(link_node));
node->data = i;
cur->next = node;
cur = node;
}
cur->next = NULL;
return head;
}
int main(){
link_node* …Run Code Online (Sandbox Code Playgroud)