simple question, I import a DLL function and the parameter are int*. When I try to enter Method(0), I get an error which says: "int and int* can not convert".
What is that meaning?
int* means a pointer to a variable whose datatype is integer. sizeof(int*) returns the number of bytes used to store a pointer. Since the sizeof operator returns the size of the datatype or the parameter we pass to it.
int *x declares a pointer to an integer value. But you need to set it to something before it is a valid value.
int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1.
int **p declares a pointer on the stack which points to pointer(s) on the heap. Each of that pointer(s) point to an integer or array of integers on the heap. This: int **p = new int*[100]; means that you declared a pointer on the stack and initialized it so that it points to an array of 100 pointers on heap.
That is classic C notation for a pointer to an int
. Whenever a type is followed by a *
, it denotes that type as a pointer to that type. In C#, unlike in C, you must explicitly define functions as unsafe
to use pointers, in addition to enabling unsafe code in your project properties. A pointer type is also not directly interchangeable with a concrete type, so the reference of a type must be taken first. To get a pointer to another type, such as an int, in C# (or C & C++ for that matter), you must use the dereference operator &
(ampersand) in front of the variable you wish to get a pointer to:
unsafe
{
int i = 5;
int* p = &i;
// Invoke with pointer to i
Method(p);
}
'Unsafe' code C#
Below are some key articles on unsafe code and the use of pointers in C#.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With