C # Cannot declare pointer variable in structure

I'm working in Visual Studio 2012. I'm just trying to create a struct with pointers to the same struct:

namespace PLT_1_lab {
    class Program {
        struct tNode {
            string oper;
            int level;
            tNode *left;
            tNode *right;
        }
    }
}

      

And on lines with * I get error (I translated VS to look a little different):

Unable to get address, determine ad pointer size for managed type ("PLT_1_lab.Program.tNote")

+3


source to share


3 answers


In C #, you usually use reference types if you need reference mechanics.

A struct is a value type, but a class is a reference type, so changing the struct to a class will do the trick:



class tNode
{
    string oper;
    int level;
    tNode left;
    tNode right;
}

      

In this context, a null reference is equivalent to a C ++ null pointer.

+7


source


Matthew's answer is probably the most appropriate way, but just to explain why the compiler is complaining ...

From the C # 5 spec, section 18.2 (pointer types):



A pointer type is written as an unmanaged type or the void keyword followed by a * token:

pointer-type:
    unmanaged-type   *
    void   *
unmanaged-type:
    type

      

The type specified before * in a pointer type is called the referent type of the pointer type. It represents the type of the variable to which the pointer type value points.

...

An unmanaged type is any type that is not a reference type or a configured type and does not contain reference types or built type fields at any nesting level. In other words, an unmanaged type is one of the following:

  • sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool.
  • Any type of enumeration.
  • Any type of pointer.
  • Any user-defined structured type that is not a built type and contains only fields of unmanaged types.

An intuitive rule for mixing pointers and references is that reference (object) referents are allowed to contain pointers, but pointer references are not allowed to contain references.

So the problem is that your structure tNode

contains a value string

. Without it - and assuming you are in an unsafe context - compiling the code.

+5


source


No, you cannot do this.

The correct way to do this is to use class

instead struct

.

Now why is this a problem in the first place?

This is documented on MSDN under Pointer Types :

A pointer cannot point to a reference or a structure containing references , since an object reference can be garbage collected even if the pointer points to it. The garbage collector does not keep track of whether an object points to any pointer types.

(my emphasis)

So, the problem here is not the pointer, as such, that the structure contains a string.

This structure works:

namespace PLT_1_lab {
    class Program {
        struct tNode {
            // string oper;
            int level;
            tNode *left;
            tNode *right;
        }
    }
}

      

So another way to fix this is to replace the pointer to string with something else, for example, something that can be marching like BStr or similar, but it all depends on why you are doing this, instead of just using class

to start.

+2


source







All Articles