This C# program demonstrates how the pointer and address of operators are used. In order to compile and run this code, the pointer operators must exist in a block of code that is designated as unsafe( like the Main() function shown here), and it must have project settings that allow unsafe code (Allow unsafe code must be checked under the Build settings).
using System; namespace XoaX { class Program { unsafe static void Main(string[] args) { int i = 7; Console.WriteLine("The value of i"); Console.WriteLine("i = " + i); int* ipPtr = &i; Console.WriteLine("The location i of in memory"); Console.WriteLine("&i = " + (int)ipPtr); Console.WriteLine("Accessing the value of i through a pointer"); Console.WriteLine("*ipPtr = " + *ipPtr); Console.WriteLine("Accessing a member of i via ->"); Console.WriteLine("ipPtr->GetType() = " + ipPtr->GetType()); Console.WriteLine("Accessing the same member of i via * and ."); Console.WriteLine("(*ipPtr).GetType() = " + (*ipPtr).GetType()); } } }
The value of i i = 7 The location i of in memory &i = 5237992 Accessing the value of i through a pointer *ipPtr = 7 Accessing a member of i via -> ipPtr->GetType() = System.Int32 Accessing the same member of i via * and . (*ipPtr).GetType() = System.Int32 Press any key to continue . . .
© 20072024 XoaX.net LLC. All rights reserved.