Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Pointer to double

I have a declaration and in the declaration, I want to set a height is a pointer to a double but get the error mesasage:

Error 1 Pointers and fixed size buffers may only be used in an unsafe context,

Can someone show me the right way to declare the type of pointer in a double ?

Below is mine declaration and I set the height to a pointer of double (double* height) but gets an error message.

private static extern bool GetElevation(double dLat, double dLon, double* height);
like image 933
Fabian Avatar asked Nov 03 '10 00:11

Fabian


Video Answer


2 Answers

Your extern declaration should probably be:

private static extern bool GetElevation(double dLat, double dLon, ref double height);

Hope this helps!

Edit

This question (and accepted answer) might shed some light on the subject. It talks about ref vs out (not sure which would fit better in your situation) and Marshalling.

like image 191
Mark Carpenter Avatar answered Oct 01 '22 06:10

Mark Carpenter


I think you should:

  1. Learn more about using pointers and what unsafe blocks are in C#, here is a good resource
  2. Mark your function as "unsafe", see below:

private static unsafe extern bool GetElevation(double dLat, double dLon, double* height)

Once all that is done then you can compile with the /unsafe switch.

like image 20
Panicos Avatar answered Oct 01 '22 06:10

Panicos