# Cannot cast char array to pointer

**URL:** https://talk.remobjects.com/t/cannot-cast-char-array-to-pointer/30902
**Category:** C#
**Created:** [October 15, 2024, 7:19am UTC](https://talk.remobjects.com/t/cannot-cast-char-array-to-pointer/30902 "2024-10-15T07:19:59Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Andreas\_Flucke](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@Andreas\_Flucke](https://talk.remobjects.com/u/Andreas_Flucke)
#### Post date: [October 15, 2024, 7:19am UTC](https://talk.remobjects.com/t/cannot-cast-char-array-to-pointer/30902/1 "2024-10-15T07:19:59Z")

</div>

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

```auto
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*"
			}
		}
	}
}

```

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [October 15, 2024, 1:57pm UTC](https://talk.remobjects.com/t/cannot-cast-char-array-to-pointer/30902/2 "2024-10-15T13:57:39Z")

</div>

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:

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

```

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [October 15, 2024, 7:01pm UTC](https://talk.remobjects.com/t/cannot-cast-char-array-to-pointer/30902/4 "2024-10-15T19:01:52Z")

</div>

Correction:

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

is the right syntax for Address of, ofc.

---

<div class="post-metadata">

### Author: ![Andreas\_Flucke](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@Andreas\_Flucke](https://talk.remobjects.com/u/Andreas_Flucke)
#### Post date: [October 16, 2024, 6:26am UTC](https://talk.remobjects.com/t/cannot-cast-char-array-to-pointer/30902/5 "2024-10-16T06:26:50Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [October 16, 2024, 12:56pm UTC](https://talk.remobjects.com/t/cannot-cast-char-array-to-pointer/30902/6 "2024-10-16T12:56:19Z")

</div>

> [@Andreas\_Flucke](#):
>
> 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…
