Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Articles » .NET Framework »
Operations that can be performed on POINTER'S
|
NOTE: Kindly go through the article regarding UNSAFE CODE before going through this one
Accessing Structure Members Through a Pointer When you access a member of a structure through a pointer, you must use the arrow operator, which is ->. For example, given this structure,
struct MyStructure { public int x; public int y; public int sum() { return x + y; } }
Here is how you would access its members through a pointer:
MyStructure o = new MyStructure(); MyStructure* pointer; // declare a pointer
pointer = &o; pointer ->x = 10; pointer ->y = 20;
Console.WriteLine( "Sum is " + pointer -> sum() );
Pointer Arithmetic
There are only four arithmetic operators that can be used on pointers: ++, --, +, -. To understand what occurs in pointer arithmetic, we will begin with an example. Let p1 be an int pointer with a current value of 2,000(that is, it contains the address 2000). After this expression,
p1++;
the contents of p1 will be 2004, not 2001! The reason is that each time p1 is incremented, it will point to the next int. Since int in C# is 4 bytes long, incrementing p1 increases its value by 4. The reverse is true for decrements. Each decrement decreases p1’s value by 4.For example,
p1--;
will cause p1 to have the value 1,996, assuming that it previously was 2,000.
Generalizing from the preceding example, each time that a pointer is incremented it will point to the memory location of the next element of its base type. Each time it is decremented, it will point to the location of the previous element of its base type. Pointer arithmetic is not limited to only increment and decrement operations. You can also add or subtract integers to or from pointers. The expression
p1 = p1 + 9;
makes p1 point to the ninth element of p1’s base type, beyond the one it is currently pointing to. Although you cannot add pointers, you can subtract one pointer from another (provided they are of the same base type). The remainder will be the number of elements of the base type that separate the two pointers. Other than addition and subtraction of a pointer and an integer, or the subtraction of two pointers, no other arithmetic operations can be performed on pointers.
Pointer Comparisons
Pointers can be compared using the relational operators, such as = =, <, and >. Here is an example that uses pointer comparison to find the middle element of an array:
using System; class ComparePointers { unsafe public static void Main() { int[] number = new int[11]; int x; //find the middle fixed ( int* start = &number[0]) { fixed ( int* end = &number[number.Length-1]) { for ( x=0;start + x <= end-x; x++); } } Console.WriteLine( "Middle element is " + x); Console.Read(); } }
Here is the output:
Middle element is 6
The pointers start and end must be created within a fixed statement because they point to elements of an array, which is a reference type.
Pointers and Arrays
In C#, pointers and arrays are related. For example, the name of an array without any index generates a pointer to the start of the array.
/* An array name without an index yields a pointer to the start of the array. */ using System; class PointerArray { unsafe public static void Main() { int[] number = new int[10]; fixed ( int* p1 = &number[0], p2 = number) { if (p1 == p2) Console.WriteLine( "p1 and p2 point to same address" ); Console.Read(); } } }
The output is shown here:
p1 and p2 point to same address.
As the output shows, the expression &nums[0] is the same asnums Since the second form is shorter, most programmers use it when a pointer to the start of an array is needed.
Indexing a Pointer
When a pointer refers to an array, the pointer can be indexed as if it were an array.
There are two important points to understand about indexing a pointer. First, no boundary checking is applied. Thus, it is possible to access an element beyond the end of the array to which the pointer refers. Second, a pointer does not have a length property. So, using the pointer, there is no way of knowing how long the array is.
Pointers and Strings
It is possible to access the characters in a string through a pointer. To do so, you will assign a pointer to the start of the string to a char* pointer using a fixed statement like this:
fixed( char* p = str ) { //…
After the fixed statement executes, p will point to the start of the array of characters that make up the string. This is null-terminated, which means that it ends with a 0.You can use this fact to test the end of an array. Here is a program that demonstrates accessing a string through a char* pointer:
// Use fixed to get a pointer to the start of a string. using System; class FixedString { unsafe public static void Main() { string aString = "this is a test"; // Point p to start of aString. fixed( char* p = aString) { //Display the contents of aString via p. for(int i = 0; p[i] != 0; i++) Console.Write( p[i] ); } Console.WriteLine(); } }
The output is shown here:
this is a test
Multiple Indirection
You can have a pointer point to another pointer that points to the target value. This situation is called multiple indirection, or pointers to pointers. In case of a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the variable that contains the value desired.
Multiple indirection can be carried on to whatever extent desired, but more than a pointer to a pointer is rarely needed. In fact, excessive indirection is difficult to follow and prone to conceptual errors.
A variable that is a pointer to a pointer must be declared as such. You do this by placing an additional * after the type name. For example, the following declaration tells the compiler that q is a pointer to a pointer of type int:
int** q;
You should understand that q is not a pointer to an integer, but rather a pointer to an int pointer. To access the target value indirectly pointed to by a pointer to a pointer, you must apply the * operator twice, as in this example:
using System; class MultipleIndirection { unsafe public static void Main() { int x; //holds an int value int* p; //holds an int pointer int** q; //holds a pointer to an int pointer
x = 10; p = &x; //put address of x int p q = &p; //put address of p int q
Console.WriteLine(**q); // display the value of x Console.Read(); } }
The output is the value x, which is 10. In the program, p is declared as a pointer to an int and q as a pointer to an int pointer.
Arrays of pointers
Pointers can be arrayed like any other data type. The declaration for an int pointer array of size 3 is
int * [] ptrs = new int * [3];
To assign the address of an int variable called var to the third element of the pointer array, write
ptrs[2] = &var;
To find the value of var, write
*ptrs[2]
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|