locked
How to declare a pointer in struct? RRS feed

  • Question

  • I am studying about algorithms (Red Black Tree), but when I declared a pointer in struct, then I had got a error:

    typedef enum {BLACK, RED} NodeColor;

    typedef int DataType;

    typedef struct NodeTag
    {
    DataType          key;
    struct NodeTag  *pLeft;
    struct NopeTag  *pRight;
    struct NodeTag  *pParent;
    NodeColor         color
    } RBNode;

    Anybody help me?
    Monday, December 24, 2007 6:13 AM

Answers

  • If you REALLY want to use pointers, you can use them if you write your code inside of an unsafe{...} block, and you modify the compiler options to allow unsafe code.

     

    However, this is the "C" way of doing things, not the "C#" way. In C# you can implement the same algorithms using object references instead of pointers. This is achieved in your source code by just writing the name of the struct in front of a field:

     

    struct NodeTag
    {
     DataType   key;
     NodeTag  pLeft;
     NopeTag  pRight;
     NodeTag  pParent;
     NodeColor  color
    }

    NodeTag RBNode;

     

    You can access the members like RBNode.pLeft.key, for instance.

    Monday, December 24, 2007 8:32 AM