Recipe19.2.Comparing Pointers


Recipe 19.2. Comparing Pointers

Problem

You need to know whether two pointers point to the same memory location. If they don't, you need to know which of the two pointers points to a higher or lower element in the same block of memory.

Solution

Using the == and != operators, you can determine if two pointers point to the same memory location. For example, the code:

 unsafe {     int[] arr = new int[5] {1,2,3,4,5};     fixed(int* ptrArr = &arr[0])     {         int* p1 = (ptrArr + 1);         int* p2 = (ptrArr + 3);         Console.WriteLine("p2 > p1");         Console.WriteLine("(p2 == p1) = " + (p2 == p1));         Console.WriteLine("(p2 != p1) = " + (p2 != p1));         p2 = p1;         Console.WriteLine("p2 == p1");         Console.WriteLine("(p2 == p1) = " + (p2 == p1));         Console.WriteLine("(p2 != p1) = " + (p2 != p1));     } } 

displays the following:

 p2 > p1 (p2 == p1) = False (p2 != p1) = True p2 == p1 (p2 == p1) = True (p2 != p1) = False 

Using the >, <, >=,or <= comparison operators, you can determine whether two pointers are pointing to a higher, a lower, or the same element in an array. For example, the code:

 unsafe {     int[] arr = new int[5] {1,2,3,4,5};     fixed(int* ptrArr = &arr[0])     {         int* p1 = (ptrArr + 1);         int* p2 = (ptrArr + 3);         Console.WriteLine("p2 > p1");         Console.WriteLine("(p2 > p1) = " + (p2 > p1));         Console.WriteLine("(p2 < p1) = " + (p2 < p1));         Console.WriteLine("(p2 >= p1) = " + (p2 >= p1));         Console.WriteLine("(p2 <= p1) = " + (p2 <= p1));         p2 = p1;         Console.WriteLine("p2 == p1");         Console.WriteLine("(p2 > p1) = " + (p2 > p1));         Console.WriteLine("(p2 < p1) = " + (p2 < p1));         Console.WriteLine("(p2 >= p1) = " + (p2 >= p1));         Console.WriteLine("(p2 <= p1) = " + (p2 <= p1));     } } 

displays the following:

 p2 > p1 (p2 > p1) = True (p2 < p1) = False (p2 >= p1) = True (p2 <= p1) = False p2 == p1 (p2 > p1) = False (p2 < p1) = False (p2 >= p1) = True (p2 <= p1) = True 

Discussion

When manipulating the addresses that pointers point to, it is sometimes necessary to compare their addresses. The ==, !=, >, <, >=, and <= operators have been overloaded to operate on pointer-type variables. These comparison operators do not compare the value pointed to by the pointers; instead, they compare the addresses pointed to by the pointers.

To compare the values pointed to by two pointers: dereference the pointers and then use a comparison operator on them. For example:

 *intPtr == *intPtr2 

will compare the values pointed to by these pointers, rather than their addresses.

See Also

See the "C# Operators," "== Operator," and "!= Operator" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net