首页 > 解决方案 > 从内存地址获取值

问题描述

我有一个int*存储内存地址的变量,例如 address 0x28c1150

我想知道,地址中存储了什么值。

编辑:

struct list {
    int value;
    list *next;
    list *head = NULL;
    void push(int n);
    void select();
    void pop();
    void top();

};

void list::push(int value) {
    list *temp = new list;

    temp->value = value;
    temp->next = head;
    head = temp;
}
void list::top(){
    list * temp = new list;

    cout << head;
}

我想打印我的列表顶部

标签: c++pointers

解决方案


如果您的变量是list*

list* variable = new list;
variable->top();

...但是请注意,您当前的top()函数会泄漏内存,因为您每次调用它时都会分配一个新列表,而您只是忘记了它。试试这个:

int list::top(){  
    return head->value;
}

std::cout << variable->top() << "\n";

推荐阅读