Originally Posted by
Elysia
A char* is interpreted as a C-style string. A string is an array consisting of N characters and a '\0' at the end. Clearly you don't have a string, so you gibberish instead. Whatever lies in the memory address at the point that you're printing it. You're lucky it doesn't crash.
You have to cast it to some other type to get it to work, eg:
Code:
cout << reinterpret_cast<int*>(pc) << endl;
Cheers, i think I understand now.
Also, could you advice on this code from the book I am using. It is showing how to use pointer to create a doubly linked list and I am completely lost.
This is the struct:
Code:
struct Link {
string value;
Link* prev;
Link* succ;
Link(const string&v, Link* p = 0; Link*s = 0)
:value(v), prev(p), succ(s) {}
};
I understand that the value is the name of the link, with prev and succ being pointers to the predecessor and successor link.
Now the bits I don't understand are twofold, regarding inserting into the list. The book shows two ways of doing this, a long way, followed by a function.
The long way is:
Code:
Link* norse_gods = new Link("tHOR", 0, 0);
norse_gods = new Link("Odin", norse_gods, 0);
norse_gods->succ->prev = norse_gods;
norse_gods = new Link("Freia", norse_gods, 0);
norse_gods->succ->prev = norse_gods;
And the function is:
Code:
Link* insert(Link*p, Link*n) //insert n before p
{
n->succ=p;
p->prev->succ = n;
n->prev=p->prev;
p->prev = n;
return n;
}
The parts that I am completely flummoxed with, in particular, is norse_gods->succ->prev = norse_gods and also understanding how the function works?
Do you have any tips regarding this?
Thanks,
Darren.