Pointers in C#

Posted on

Pointers in C#

C# permits using pointer variables in a method of code slab when it is strike by the unsafe converter. The unsafe code or the unmanaged cipher is a data slab that uses a pointer variable.

Note: To execute the programs, put compilation alternative in Project >> Compile Options >>

Compilation Command to

mcs *.cs -out:main.exe -unsafe”

Pointers

A pointer is a variable whose value is the location of extra variable that is, the right address of the memory location. alike to any variable or constant, you must state a pointer formerly you can custom it to save any variable address.

The broad outline of a pointer statement is:

type *variable-name;

Following are acceptable pointer declarations:

int *p; /* pointer to an integer */

double *p; /* pointer to a double */

float *p; /* pointer to a float */

char *h /* pointer to a character */

The following paradigm shows use of pointers in C#, by means of the unsafe modifier:

using System;

namespace UnsafeCodeApplication

{

class Pointerprogram

{

static unsafe void Main(string[] args)

{

int a = 20;

int* p = &a;

Console.WriteLine(“Data: {0} “, a);

Console.WriteLine(“Address : {0}”, (int)p);

Console.ReadKey();

}

}

}

When the overhead enigma was compiled and carried out, it generates output as:

Data : 200

Address : 99215

Rather than declaring an entire function/code as unsafe, you can also state a fragment of the code as insecure/unsafe. The example in the subsequent fragment illustrate this concept.

Displaying the Value by means of a Pointer

You can repossess the data stowed at the sited position by the pointer variable, using the ToString() method.

The following exemplar proves this:

using System;

namespace UnsafeCodeApplication

{

class Valuepointer

{

public static void Main()

{

unsafe

{

int a = 20;

int* pointer = &a;

Console.WriteLine(“Data: {0} ” , a);

Console.WriteLine(“Data: {0} ” , pointer->ToString());

Console.WriteLine(“Address: {0} ” , (int)pointer);

}

Console.ReadKey();

}

}

}

When the overhead program was compiled and run, it generates the ensuing output:

Data: 20

Data: 20

Address: 77128984

Tossing Pointers as Parameters to Procedures

You can toss a pointer member to a procedure as parameter. The following example demonstrates this:

using System;

namespace UnsafeCodeApplication

{

class TPointer

{

public unsafe void swapp(int* p, int *q)

{

int temp = *p;

*p = *q;

*q = temp;

}

public unsafe static void Main()

{

TPointer p = new TPointer();

int var1 = 10;

int var2 = 20;

int* x = &var1;

int* y = &var2;

Console.WriteLine(“Before Swap: var1:{0}, var2: {1}”, var1, var2);

p.swapp(x, y);

Console.WriteLine(“After Swap: var1:{0}, var2: {1}”, var1, var2);

Console.ReadKey();

}

}

}

Output:

Before Swap: var1: 10, var2: 20

After Swap: var1: 20, var2: 10

Retrieving Array Elements by means of a Pointer

In C#, an array and a pointer to a data type same as the array data,but not of the same data type. Example, int *q and int[] q, are not same type. You can increase the pointer variable q because it is not fixed in memory but in case of array data you can’t increment that.

Therefore, if you want to read an array data using a pointer variable, as done in C or C++, you have to setup the pointer using the fixed keyword.

The following example demonstrates this:

using System;

namespace UnsafeCodeApplication

{

class TestPointer

{

public unsafe static void Main()

{

int[] list = {10, 100, 200};

fixed(int *ptr = list)

/* let us have array address in pointer */

for ( int i = 0; i < 3; i++)

{

Console.WriteLine(“Address of list[{0}]={1}”,i,(int)(ptr + i));

Console.WriteLine(“Value of list[{0}]={1}”, i, *(ptr + i));

}

Console.ReadKey();

}

}

}

Result:

Address of list[0] = 31627168

Value of list[0] = 10

Address of list[1] = 31627172

Value of list[1] = 100

Address of list[2] = 31627176

Value of list[2] = 200

Executing Unsafe Code

For compiling unsafe code, you have to identify the /unsafe command-line switch with command-line compiler.

To execute a code named prog.cs surrounding unsafe code, give the command:

csc /unsafe prog1.cs

If you are utilizing Visual Studio IDE then you require to assist aid of unsafe code in the project properties.

To do this:

 Open project properties.

 Click on Build.

 Select “Allow unsafe code”.

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s