为什么相同指针的地址不同?

发布于 2025-02-05 06:03:45 字数 1260 浏览 4 评论 0原文

我正在尝试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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心的憧憬 2025-02-12 06:03:46

HEAD是一个变量,head_1是另一个变量。这些变量都位于内存中的不同位置。您正在打印出它们居住的位置。

这些变量恰好是指针类型,这些变量的有效载荷将相同(链接列表的头部的地址)。但是您要打印的变量的实际位置将有所不同。

head is a variable and head_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.

酒浓于脸红 2025-02-12 06:03:46

函数参数按值传递,即制作副本。仅当您将论点声明为参考时,就会传递参考(即无副本)。

考虑以下示例:

#include <iostream>


void foo(int* a) {
    std::cout << "value of a:    " << a << "\n";
    std::cout << "address of a:  " << &a << "\n";
    std::cout << "a dereference: " << *a << "\n";
}
void bar(int* const& b) {
    std::cout << "value of b:    " << b << "\n";
    std::cout << "address of b:  " << &b << "\n";
    std::cout << "b dereference: " << *b << "\n";
}


int main(){
    int x = 42;
    int* ptr = &x;
    std::cout << "value of ptr:    " << ptr << "\n";
    std::cout << "address of ptr:  " << &ptr << "\n";
    std::cout << "ptr dereference: " << *ptr << "\n";

    
    foo(ptr);
    bar(ptr);
}

可能的输出

value of ptr:    0x7fff77c582cc
address of ptr:  0x7fff77c582c0
ptr dereference: 42
value of a:    0x7fff77c582cc
address of a:  0x7fff77c582a8
a dereference: 42
value of b:    0x7fff77c582cc
address of b:  0x7fff77c582c0
b dereference: 42

ptra a a /code>和b全部指向相同的int。所有人都有相同的值。将它们解释所有都会导致相同的int,它是x,带有Value 42。但是,aptr的副本,并存储在不同的地址。因为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:

#include <iostream>


void foo(int* a) {
    std::cout << "value of a:    " << a << "\n";
    std::cout << "address of a:  " << &a << "\n";
    std::cout << "a dereference: " << *a << "\n";
}
void bar(int* const& b) {
    std::cout << "value of b:    " << b << "\n";
    std::cout << "address of b:  " << &b << "\n";
    std::cout << "b dereference: " << *b << "\n";
}


int main(){
    int x = 42;
    int* ptr = &x;
    std::cout << "value of ptr:    " << ptr << "\n";
    std::cout << "address of ptr:  " << &ptr << "\n";
    std::cout << "ptr dereference: " << *ptr << "\n";

    
    foo(ptr);
    bar(ptr);
}

Possible output:

value of ptr:    0x7fff77c582cc
address of ptr:  0x7fff77c582c0
ptr dereference: 42
value of a:    0x7fff77c582cc
address of a:  0x7fff77c582a8
a dereference: 42
value of b:    0x7fff77c582cc
address of b:  0x7fff77c582c0
b dereference: 42

ptr, a and b all point to the same int. The all have the same value. Dereferencing them all leads to the same int, it is x with value 42. However, a is a copy of ptr and is stored at different address. Because b is passed by reference, taking its address yields the address of ptr.

皓月长歌 2025-02-12 06:03:45

HEADHEAD_1是两个不同的变量,因此它们具有两个不同的地址。但是这些变量的也是地址(或指针),这些值是相同的。

当您处理指针时,很容易获得变量的地址和变量混合的值,因为它们都是指示器(但指针类型不同)。

换句话说,所有变量都有地址,但是只有指针变量具有也是地址的值。

改用此代码

cout<<"The value of the head pointer: "<<head<<endl;

cout<<"The value of the head_1 pointer: "<<head_1<<endl;

head and head_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

cout<<"The value of the head pointer: "<<head<<endl;

cout<<"The value of the head_1 pointer: "<<head_1<<endl;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文