I'm trying to make a simple linked list example. I am using the dynamic method of memory allocation.

#include <iostream.h>

struct node
{
    int INSTRUCTION;
    node * next;

    node()
    {   next = NULL; INSTRUCTION = 0;   }

    void setnext(node *P)
    {   next = P;   }

    void DELETE()
    {   delete this;    }
};

void main()
{
    node *A = new node();
    node *B = new node();
    A->setnext(B);
    B->setnext(NULL);
    cout << &A << endl << A->next << endl << &B << endl << B->next;
    delete A, B;
}

In the code, &B and A->next should have the same value, but for some reason I am getting different addresses printed on the screen.

OOps. I found the error. B and A->next will have same values (and not &B and A->next).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.