Character pointer is allowed to alias to any other pointer type.
If you cast your .UserData
pointer to the correct type( typeof MyDataStruct ), you can use the object it points to.
You are also allowed to dereference a character pointer without casting as long as the pointer points to valid memory. What value the memory holds is another matter and you should use it correctly.
And a quote why is it allowed:
6.5. p7 An object shall have its stored value accessed only by an lvalue expression that has one of
the following types:
— a type compatible with the effective type of the object,
— a qualified version of a type compatible with the effective type of the object,
— a type that is the signed or unsigned type corresponding to the effective type of the
object,
— a type that is the signed or unsigned type corresponding to a qualified version of the
effective type of the object,
— an aggregate or union type that includes one of the aforementioned types among its
members (including, recursively, a member of a subaggregate or contained union), or
— a character type.
And:
6.2.5. p28 A pointer to void shall have the same representation and alignment requirements as a
pointer to a character type
6.3.2.3. p1 A pointer to void may be converted to or from a pointer to any object type. A pointer to
any object type may be converted to a pointer to void and back again; the result shall
compare equal to the original pointer.
Therefore a char pointer can store a pointer of any type, the same way as a void pointer.
Of course as you mention with your reference, none of this holds true for function pointers. You cannot mix them and other types.