0

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.

Edited by deceptikon: Moved to C++

1
Contributor
1
Reply
10
Views
4 Years
Discussion Span
Last Post by nmakes
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.