为什么相同指针的地址不同?
我正在尝试C ++上的链接列表,当我尝试打印列表的数据时,它给出了有趣的结果,这告诉我列表为null。当我将变量的地址打印在MAIM中并打印功能中同一变量的参数的地址时,它给出了不同的结果。谁能为我解释,谢谢! 这是代码:
#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* head_1 = create(0);
link_node* head_2 = create(1);
link_node* result = (link_node*) malloc(sizeof(link_node));
cout<<"The address of head_1: "<<&head_1<<endl;
iter(head_1);
return 0;
}
这是程序的输出:
The address of head_1: 0x61ff04
The address of head in function: 0x61fef0
0 2 4 6 8
谢谢!
I was trying linked list on C++ and when I try to print the data of the list, it gives funny result which tells me that the list is null. When I print the address of the variable in main and print the address of the parameter of the same variable in the function, it gives different results. Could anyone please explain it for me, thank you!
Here is the code:
#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* head_1 = create(0);
link_node* head_2 = create(1);
link_node* result = (link_node*) malloc(sizeof(link_node));
cout<<"The address of head_1: "<<&head_1<<endl;
iter(head_1);
return 0;
}
And here is the output of the program:
The address of head_1: 0x61ff04
The address of head in function: 0x61fef0
0 2 4 6 8
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HEAD
是一个变量,head_1
是另一个变量。这些变量都位于内存中的不同位置。您正在打印出它们居住的位置。这些变量恰好是指针类型,这些变量的有效载荷将相同(链接列表的头部的地址)。但是您要打印的变量的实际位置将有所不同。
head
is a variable andhead_1
is a another variable. These variables both reside at different locations in memory. You are printing out the locations where they reside.These variables, both happen to be pointer types, and the payload of these variables will be the same (the address of the head of your linked list). But the actual location of the variables, which you are printing out, will be different.
函数参数按值传递,即制作副本。仅当您将论点声明为参考时,就会传递参考(即无副本)。
考虑以下示例:
可能的输出:
ptr
,a
a
a
/code>和
b
全部指向相同的int
。所有人都有相同的值。将它们解释所有都会导致相同的int
,它是x
,带有Value42
。但是,a
是ptr
的副本,并存储在不同的地址。因为b
是通过引用传递的,因此将其地址传递给ptr
的地址。Function parameters are passed by value, ie a copy is made. Only if you declare the argument to be a reference, a reference (ie no copy) is passed.
Consider the following example:
Possible output:
ptr
,a
andb
all point to the sameint
. The all have the same value. Dereferencing them all leads to the sameint
, it isx
with value42
. However,a
is a copy ofptr
and is stored at different address. Becauseb
is passed by reference, taking its address yields the address ofptr
.HEAD
和HEAD_1
是两个不同的变量,因此它们具有两个不同的地址。但是这些变量的值也是地址(或指针),这些值是相同的。当您处理指针时,很容易获得变量的地址和变量混合的值,因为它们都是指示器(但指针类型不同)。
换句话说,所有变量都有地址,但是只有指针变量具有也是地址的值。
改用此代码
head
andhead_1
are two different variables so they have two different addresses. But the values of those variables are also addresses (or pointers) and those values are the same.When you are dealing with pointers it's easy to get the address of a variable and the value of a variable mixed up because they are both pointers (but different pointer types).
Put it another way, all variables have addresses, but only pointer variables have values which are also addresses.
Try this code instead