What are pointer data types in C#?


A pointer is a variable whose value is the address of another variable i.e., the direct address of the memory location. Similar to any variable or constant, you must declare a pointer before you can use it to store any variable address.

The syntax of a pointer is −

type *var-name;

The following is how you can declare a pointer type −

int *ip; /* pointer to an integer */
double *dp; /* pointer to a double */

C# allows using pointer variables in a function of code block when it is marked by the unsafe modifier. The unsafe code or the unmanaged code is a code block that uses a pointer variable.

Here is the module showing how to declare and use a pointer variable. We have used unsafe modifier here −

static unsafe void Main(string[] args) {
   int var = 20;
   int* p = &var;
   
   Console.WriteLine("Data is: {0} ", var);
   Console.WriteLine("Address is: {0}", (int)p);
   Console.ReadKey();
}

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 20-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements