Posted by Stu on July 18, 2000 at 20:11:31:
In Reply to: HW8 posted by Mike on July 18, 2000 at 19:17:07:
:
: Ok, I got no quams with this. However, in class you also defined a new variable like this (unless I copied wrong):
: NodeType a;
: a = new NodeType;
You must have copied it wrong (we'll assume so) :)
Basics are:
// C-code snippet
NodeType a; // a is a 8-byte NodeType struct
NodeType* pa; // pa is an uninitialized 4-byte
// ptr that _can_ point to a
// NodeType struct but doesn't yet.
pa = NULL ; // Now pa just points to address 0.
// Still doesn't point at a valid
// NodeType in memory somewhere yet.
pa = new NodeType ; // Now it points to a real
// NodeType struct.
pa->i = 42 ; // Now we can safely
pa->Next = NULL ; // dereference it. Happy fun!
// Fini la snippet
: The book does something similar:
: NodeType* a = NULL;
: a = new NodeType;
: However, I can't get either to compile.
This code works fine given the original struct def. You must have mistyped something. Post an error message if you can't get it to work.
: However, this does [work]:
: NodeType* Head = new NodeType;
Yep. Same syntax as:
NodeType* a = new NodeType ;
: I was wondering, can I assume that the above is a Head that points to nothing (not even NULL)?
The 'new' operator returns a pointer. When you say:
NodeType* Head = new NodeType;
... you are assigning to 'Head', the pointer to the memory that was just allocated for the new NodeType struct you requested.
: Also, is #define NULL 0
Heh - couldn't see