Class 12 : Visual Programming : Unit 6: Pointers in C#
6.1 Introduction to Pointers
A pointer is a variable that stores the memory address of another variable. Unlike an ordinary variable, which stores a value, a pointer stores the location where that value is stored in memory.
Pointers are mainly used when direct access to memory is required. In C#, pointers are used in unsafe code, so the unsafe keyword is required when declaring or using pointer variables.
Important Terms
- Variable: A named memory location that stores a value.
- Address: The location of a variable in computer memory.
- Pointer: A variable that stores the address of another variable.
- Dereferencing: Accessing the value stored at the memory address held by a pointer.
- Unsafe code: C# code that allows direct memory manipulation using pointers.
Basic Syntax of Pointers
Â
unsafe
{
int x = 10;
int* p = &x;
}
Â
In this example, x is an integer variable that stores the value 10. The expression &x is used to obtain the memory address of the variable x. The pointer variable p, declared as int*, stores this memory address and therefore points to the location where x is stored. The expression *p is called the dereference operator, which accesses the actual value stored at the memory address contained in p. Thus, *p gives the value 10 stored in x. The code is placed inside an unsafe block because pointer operations in C# require explicit permission to perform memory-level operations.
Pointer Operators
| Operator | Name | Purpose |
|---|---|---|
& | Address-of operator | Obtains the memory address of a variable |
* | Dereference operator | Obtains the value stored at a pointer address |
-> | Pointer member access | Accesses members through a pointer |
Example : Write a C# program using an unsafe pointer to display the value of an integer variable both directly and through a pointer.
using System;
class PointerDemo
{
public static void Main()
{
unsafe
{
int number = 25;
int* ptr = &number;
Console.WriteLine("Value = " + number);
Console.WriteLine("Value using pointer = " + *ptr);
}
}
}
Output :
Value = 25
Value using pointer = 25
6.2 Advantages and Disadvantages of Pointers
Pointers provide direct access to memory and can be useful in certain programming situations. However, they also introduce additional risks and complexity.
Advantages of Pointers
- Â Direct Memory Access : Pointers allow a program to directly access a particular memory location. This can be useful for low-level programming.
- Â Faster Data Processing : Pointers can sometimes improve performance by allowing data to be accessed or modified directly without unnecessary copying.
- Â Efficient Parameter Passing : Pointers can be used to pass the address of data to a method, allowing the method to work with the original data.
- Â Useful for Array Processing: Pointers can be used to move through array elements efficiently, particularly in low-level or performance-sensitive programming.
- Â Useful in System Programming: Pointers are useful when interacting with low-level system resources, native code, and unmanaged memory.
- Memory Manipulation: Pointers provide greater control over memory, which can be useful when working with unmanaged resources.
Â
Disadvantages of Pointers
- Unsafe :Improper use of pointers can cause memory-related problems and make programs less safe.
- Difficult to Understand: Pointer concepts such as addresses, dereferencing, and pointer arithmetic can be difficult for beginners.
- Programming Errors: Incorrect pointer operations may result in unexpected values or runtime problems.
- Difficult Debugging: Programs that use pointers can be more difficult to debug than programs using normal managed variables.
- Security Risk:Improper memory access can potentially create security vulnerabilities.
- Â Limited Use in Normal C# Programming: Most C# programs do not need pointers because C# provides managed references, arrays, classes, and other higher-level features.
6.3 Retrieving the Data Value Using a Pointer
A pointer can be used to retrieve the value of a variable from its memory address. The dereference operator (*) is used to retrieve the data value stored at the address contained in a pointer.
Write a C# program using a pointer to retrieve and display the value of an integer variable through its memory address. The program should display both the original value and the value accessed using the pointer.
using System;
class RetrieveValue
{
public static void Main()
{
unsafe
{
int number = 50;
int* ptr = &number;
Console.WriteLine("Original value = " + number);
Console.WriteLine("Value using pointer = " + *ptr);
}
}
}
Output:Original value = 50 Value using pointer = 50
In this program, the integer variable number is initialized with the value 50. The statement int* ptr = &number; declares an integer pointer ptr and stores the memory address of number in it. The & operator is used to obtain the address of the variable, while the * operator is used to dereference the pointer and retrieve the value stored at that address. Therefore, *ptr accesses the value 50 through the pointer. The program displays the original value directly using number and then displays the same value using the pointer *ptr. Since pointer operations are used, the code must be written inside an unsafe block.
Modifying Data Through a Pointer
A pointer can be used not only to access a variable’s value but also to modify it. By using the dereference operator *, a new value can be assigned to the memory location pointed to by the pointer.
Example: Write a C# program using an unsafe pointer to modify the value of an integer variable through its pointer and display the modified value.
using System;
class PointerModify
{
public static void Main()
{
unsafe
{
int number = 20;
int* ptr = &number;
*ptr = 100;
Console.WriteLine("Number = " + number);
}
}
}
out put:
Number = 100
6.4 Passing Pointers as Parameters to Methods
A pointer can be passed as a parameter to a method, allowing the method to directly access or modify the value stored at a particular memory address. A pointer parameter is declared using the * symbol along with the data type, such as int*. This technique allows a method to work directly with the memory location of a variable. Since pointer operations are involved, the method and its calling code must be used in an unsafe context.
Syntax:
unsafe static void MethodName(int* ptr)
{
// statements
}
Write a C# program to pass a pointer as a parameter to a method and modify the value of an integer variable through the pointer. The program should change the value from 20 to 100 and display the modified value.
using System;
class PointerParameter
{
unsafe static void ChangeValue(int* ptr)
{
*ptr = 100;
}
public static void Main()
{
unsafe
{
int number = 20;
ChangeValue(&number);
Console.WriteLine("Number = " + number);
}
}
}
Â
Table of Contents
Toggle