Pointer

Pointer keeps the memory address of arrays and value types. Garbage Collector does not track pointer types. (You can see more detail here about Garbage Collector). Unsafe context is used to run code outside the control of Garbage Collector.

Here is some basic info about pointers,

  • int* a => a is a pointer to an integer.
  • &x => address of x.

 

Let’s start a simple example to see how pointers work.

 

using System;

namespace ZeroToCSharp {
    class Program {
        static void Main(string[] args) {

            unsafe {
                int number = 10;

                int* pointer1 = &number;      //&number => address of 10 and pointer1 is a pointer for address of 10

                int address = (int)pointer1;  //(int)pointer1 => address of 10
                int value = *pointer1;        // *pointer1 => 10 

                //To print output
                Console.WriteLine(string.Concat("Address: ", address));
                Console.WriteLine(string.Concat("Value: ", value));
                Console.ReadKey();
            }
        }
    }
}

 

Output:

Address: -1583552556
Value: 10

 

 

Here is another example about using pointers for value types.

 

using System;

namespace ZeroToCSharp {
    class Program {
        static void Main(string[] args) {

            unsafe {
                int number1 = 10;
                int number2 = 20;
                int* pointer1 = &number1;
                int* pointer2 = &number2;

                //To print output
                Console.WriteLine(string.Concat("Number1: ", *pointer1, " and its memory adress is: ", (int)pointer1));
                Console.WriteLine(string.Concat("Number2: ", *pointer2, " and its memory adress is: ", (int)pointer2));
                Console.ReadKey();
            }
        }
    }
}

 

Output:

Number1: 10 and its memory adress is: -2038962188
Number2: 20 and its memory adress is: -2038962192

 

 

Here is another example about using pointers for array.

 

using System;

namespace ZeroToCSharp {
    class Program {
        static void Main(string[] args) {
            
            unsafe {
                int[] numbers = new int[4] { 50, 60, 70, 80 };

                fixed(int* pointer = &numbers[0]) {
                    int* ptr = pointer;
                    Console.WriteLine(*ptr);
                    
                    ptr++;
                    Console.WriteLine(*ptr);
                    
                    ptr++;
                    Console.WriteLine(*ptr);
                    
                    Console.WriteLine("--------");
                    
                    Console.WriteLine(*pointer);
                    
                    (*pointer)++;
                    Console.WriteLine(*pointer);
                    
                    (*pointer)++;
                    Console.WriteLine(*pointer);
                    Console.ReadKey();
                }
            }
        }
    }
}

 

Output:

50
60
70
--------
50
51
52

 

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x