Basics Of Pointers In C#

In this article, I am going to share information about Pointers in C#:
 
Pointers in C#
 
A pointer is simply a variable whose value is the address of another variable. We can not use pointers directly. To use a pointer, we need to have a specific block for it. The 'unsafe' keyword defines the scope to use pointer variables. Pointer variables can only be used inside an unsafe code block.
 
The syntax of a pointer variable is shown below.

datatype *<Variable_Name>;
 
For example,
  1. int* a;       // 'a' is a pointer to an Integer  
  2. char* b;    // 'b' is a pointer to an Char  
  3. float* c;   // 'c' is a pointer to an Float  
Uses of pointers 
  1. When interacting with unmanaged APIs or code
  2. When we have to make communication between two application using IPC 
A method with unsafe keyword:
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         static unsafe void Main(string[] args)  
  8.         {  
  9.             // variable 'p' of int type is initialised with value 100  
  10.             int p = 100;  
  11.   
  12.             // pointer variable 'q' contains the address of variable 'p' and, '&' used to obtain address of a variable  
  13.             int* q = &p;  
  14.   
  15.             Console.WriteLine("Value: " + p);  
  16.             Console.WriteLine("Address: " + (int)q);  
  17.             Console.ReadKey();  
  18.         }  
  19.     }  
  20.   
  21. }  
Output

Value: 100
Address: 122743464
 
If we don't want to use the unsafe keyword on method definition, then there is another option through which we can use pointer variables inside a method. Have a look at the below example.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class TutLovers  
  6.     {  
  7.         public void Program()  
  8.         {  
  9.             int z = 50;  
  10.             Console.WriteLine("Before unsafe code block");  
  11.             unsafe  
  12.             {  
  13.                 // variable 'p' of int type is initialised with value 100  
  14.                 int p = 100;  
  15.   
  16.                 // pointer variable 'q' contains the address of variable 'p'  
  17.                 int* q = &p;  
  18.   
  19.                 // pointer variable 'r' contains the address of variable 'z'  
  20.                 int* r = &z;  
  21.             }  
  22.             Console.WriteLine("After unsafe code block");  
  23.   
  24.             // This will show an error because pointer variable 's' is used outside unsafe block  
  25.             // Error as "pointers and fixed size buffers may only be used in an unsafe context"  
  26.             int* s = &z;  
  27.   
  28.         }  
  29.     }  
  30. }  
Retrieving data from pointer variable:
 
There are some ways through which we can retrieve the value from a pointer variable, as shown below.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         static unsafe void Main(string[] args)  
  8.         {  
  9.             // variable 'p' of int type is initialised with value 100  
  10.             int p = 100;  
  11.   
  12.             // pointer variable 'q' contains the address of variable 'p'  
  13.             int* q = &p;  
  14.   
  15.             Console.WriteLine("Value: " + p);  
  16.   
  17.             // First way to retreive value  
  18.             Console.WriteLine("Value: " + *q);  
  19.             // Second way to retreive value  
  20.             Console.WriteLine("Value: " + q->ToString());  
  21.             Console.WriteLine("Value: " + (int)q);  
  22.             Console.ReadKey();  
  23.         }  
  24.     }  
  25.   
  26. }  
Output

Value: 100
Value: 100
Value: 100
Value: 113044312
 
Pointer variable as Method's parameter
 
Pointer variables can be used as parameters of a method, as shown below,
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     unsafe class Program  
  6.     {  
  7.         unsafe int* ss;  
  8.         static unsafe void Main(string[] args)  
  9.         {  
  10.             int value1 = 100;  
  11.             int value2 = 500;  
  12.             int* q = &value1;  
  13.             int* r = &value2;  
  14.             //Method is called with pointers as parameter  
  15.             Program.sample(q, r);  
  16.             Console.ReadKey();  
  17.         }  
  18.   
  19.         public static unsafe void sample(int* a, int* b)  
  20.         {  
  21.             int x = *a;  
  22.             int y = *b;  
  23.             Console.WriteLine("X Value: " + x);  
  24.             Console.WriteLine("Y Value: " + y);  
  25.   
  26.         }  
  27.   
  28.     }  
  29.   
  30. }  
Output

X Value: 100
Y Value: 500
 
Note
  1. In order to compile the code, we have to check the "allow unsafe code" on Project-> Right Click-> Properties-> Build tab otherwise, an error will produce as "Unsafe code may only appear if compiling with /unsafe".
  2. void* <variable> can also be used but usually, it is not recommended to use. It is basically a pointer of unknown type
  3. Use of unsafe code enhances security risks.
Conclusion
 
Pointers should only be used when it is really necessary because we are going out of security on risk. Also, we have to make a compromise with the automatic garbage collector.
Next Recommended Reading Function Pointer in C#