Cannot cast char array to pointer

Hi, this piece of code doesn’t compile with Hydrogene but with csc:

using System.Runtime.InteropServices;

namespace Test
{
	[StructLayout(LayoutKind.Explicit)]
	unsafe struct IMAGE_IMPORT_BY_NAME
	{
		[FieldOffset(0)]
		public ushort Hint;
		[FieldOffset(2)]
		public fixed char Name[1];
	}

	class Test
	{
		void TestFunc()
		{
			unsafe
			{
				char* szImportName;
				IMAGE_IMPORT_BY_NAME* pIBN = (IMAGE_IMPORT_BY_NAME*)null;
				szImportName = (char*)pIBN->Name; // RO: Cannot cast from "Char[0..0]" to "Char*"
			}
		}
	}
}

Hmm. The error seems legit to me, for .NET; a char array is NOT a pointer to a Char.

you’d need use * to get the address of the array, to achieve what you want, but that also doesnt seem to be supported:

szImportName = (char*)(*(pIBN->Name)); // E118 Cannot use pointer dereferencing on "Char[0..0]"

Correction:

szImportName = (char*)(&(pIBN->Name));

is the right syntax for Address of, ofc.

Thank you, that works.
Unfortunately this is incompatible with csc, which complains about this statement.

Which is weird, be cause the above code is wrong, and at the very least (even if it compiles) should not do what you think it does…

1 Like