0

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!

UnholySheep
  • 3,967
  • 4
  • 19
  • 24
JPPPPP
  • 23
  • 3

3 Answers3

5

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;
john
  • 85,011
  • 4
  • 57
  • 81
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.

Phillip Ngan
  • 15,482
  • 8
  • 63
  • 79
1

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.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185