-3

I have an int* variable that stores memory address, for the example address 0x28c1150.

I want to know, what value is stored at the address.

edit:

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;
}

i want to print top of my list

NoobXDDD
  • 1
  • 3

3 Answers3

1

If your variable is a list*:

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

...but note that your current top() function leaks memory since you allocate a new list every time it's called and you just forget about it. Try this instead:

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

std::cout << variable->top() << "\n";
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Thanks a lot that works fine, but can you tell me how to use this value in my code (in if to compare something) – NoobXDDD Mar 20 '19 at 09:52
  • `int other_value = 10; if(variable->top() == other_value) ...` ? I think you should edit your original question to make it clear what you are actually asking about. Right now it's put on hold. – Ted Lyngmo Mar 20 '19 at 11:17
0

You have to dereference the pointer:

template<class T>
void print_value_at(T* pointer) {
    T& value = *pointer; //get value in pointer
    // print value 
    std::cout << value <<std::endl;
}

If the pointer is void, you have to cast it to whatever type it was originally:

int x = 10;
// get void pointer to x
void* x_pointer_as_void = (void*)&x; 
// convert it back to a pointer to an int:
int* x_pointer = (int*)x_pointer_as_void;
Alecto Irene Perez
  • 10,321
  • 23
  • 46
0

Here it is (based on current version of question)

cout << head->value;
john
  • 85,011
  • 4
  • 57
  • 81