Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DotNet - What is int*?

Tags:

c#

.net

pointers

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?

like image 642
PassionateDeveloper Avatar asked Jul 13 '10 15:07

PassionateDeveloper


People also ask

What does int * mean in C?

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.

Is int * a pointer?

int *x declares a pointer to an integer value. But you need to set it to something before it is a valid value.

What is the type of int *?

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.

What is meaning of int * P______?

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.


1 Answers

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#.

  • Unsafe contexts
  • Pointer Types
  • Fixed and movable variables
  • Pointer conversions
  • Pointers in expressions
  • The 'fixed' statement
  • Stack allocation
  • Dynamic memory allocation
like image 84
jrista Avatar answered Sep 17 '22 18:09

jrista