You are on page 1of 12

(Po i n t e r s a n d Re f e r e n c e )

Email:-nabil299@gmail.com
pointers

Int *npntr
Double *ypntr ,*xpntr

cpntr Char

y Ypntr

Int y = 3
Int *ypntr
y Ypntr

int y

int ypntr

ypntr Int
y Ypntr
y address ypntr
Y

ypntr = 800 y Ypntr


y

Y=3
Ypntr = 800
y
ypntr =y

y=3

ypntr = 5

y=5

Int *ypnt

ypntr =y
Pointer

Pointers
.Net C#
Compile C#

Pointers
Pointers

C++ C Pointers

Jagged Arrays Dynamic Array


Late or Dynamic Binding Strings Array
Pointers
Dynamically

C++ C
Garbage Collector Java
GC

Pointers
C++ C

C#

// NormalCopy.cs

using System;

public class NormalCopy

public static void CopyArray(byte [] Src, byte [] Dst)

for (int j = 0; j < 10000; ++j)

for (int i = 0; i < Src.Length; ++i)

Dst[i] = Src[i];

public static void Main()

byte [] MySrcArray = new byte[1000];

byte [] MyDstArray = new byte[1000];


for (int i = 0; i < MySrcArray.Length; ++i)

MySrcArray[i] = (byte) i;

CopyArray(MySrcArray, MyDstArray);

Console.Read();

byte Array

Loop

Pointers
Java GC C#
Pointers
Pointers
C++ C

Pointers
C#
C#
C# Pointers

.Net COM DLL


C# Pointers

C++ C
Libraries Java
.Net Infrastructure Classes
Pointers
unsafe C# Pointers
Code Reserved word or Keyword
Members Pointers
Constructors Functions Properties
Classes

// Using 'unsafe' as block in a function


void MyFunction()

{
...
unsafe

{
// Unsafe code to be here
}
...
}
// Using 'unsafe' in function declaration
unsafe void MyFunction()

{
// Do something
}
// Using 'unsafe' in class declaration
unsafe class MyClass
{
// Class body to be here
}

.Net unsafe
.Net
.Net unsafe Pointers

Intermediate Language unsafe Code


Assembly Source Code IL
unsafe unsafe Assembly dll exe
Assembly .Net
unsafe Code C# Compile C#
Compiler C# C# /unsafe

csc /unsafe MyFile.cs

MyFile.cs Compiler C# C# csc


IL Compile

Pointers

C++ C C# Pointer Types


Pointer Data Type
C# Pointers
C# Objects Type Safe
C++ Pointer to Object

MyClass MyObject = new MyClass(); // in C#


MyClass * MyObject = new MyClass(); // in C++

C#

Pointer void *
type *

type

bool, sbyte, byte, short, ushort, int, uint, Value Types


long, ulong, char, float, double, decimal, enum
Pointer Pointer to Pointer Types
User-Define Types
Value Classes Structures
Reference Pointing to Types
Types
Reference Types Arrays
Arrays
Structures
Reference Types
byte * pMyByte; // Pointer to byte bool * pMyBool; // Pointer to bool
int * * pMyInt; // Pointer to pointer to int
long * [] pMyLong; // Array of pointers to long
void * pMyVoid; // Pointer to unknown type
char * pC1, pC2; // Two Pointers to char

// string * pMyString; // Error, 'string' is reference type


// The compiler generates the following error message:
// Indirection to managed type is not valid

byte MyByte = 10; // byte variable

pMyByte = & MyByte; // pMyByte now points to MyByte (i.e. The value
// of pMyByte is the address of MyByte)

// Array of bool
bool [] MyBool = {true, false, false, true};
// pMyBool = MyBool // Error, arrays is also reference types

// The compiler generates the following error message:

// Cannot implicitly convert type 'bool[]' to 'bool*'


// To point to an array use 'fixed' statement
fixed (bool * pB = MyBool)

for (int i = 0; i < MyBool.Length; ++i)

Console.WriteLine((pB + i)->ToString());

// pB++; // Error, pB is fixed


// The compiler generates the following error message:
// Cannot assign to 'pB' because it is read-only
}
// Print the value of MyByte (Will prints 10)

Console.WriteLine("MyByte is: {0}", * pMyByte);

// Another way to get the value of MyByte (Will prints


10) Console.WriteLine("MyByte is: {0}", pMyByte->ToString());

Name Variable Type Name


pC1, pC2 char C++ C
C++ C char *
pC2 char pC1 char Pointer
Pointer Indirection Operator
C# C++ C Pointer

T T*
Pointer Types Object
Operator -> Operator
pMyByte->ToString()
Reference Types Arrays
GC

Object fixed Pointing to Arrays


GC fixed GC
Object
Pointer Object
fixed

Pointers
Array NormalCopy.cs
MSDN

// FastCopy.cs

using System;

public class FastCopy


{

public static unsafe void CopyArray(byte [] Src, byte [] Dst)


{

fixed (byte * pSrc = Src, pDst = Dst)


{
byte * pS = pSrc;
byte * pD = pDst;
int Count = Src.Length;

for (int j = 0; j < 10000; ++j)


{

for (int i = Count >> 2; i != 0; --i)


{

* ((int *) pD) = * ((int *) pS);

pD += 4;
pS += 4;
}

for (Count &= 3; Count != 0; --Count)


{

* pD = * pS;
pD++;
pS++;

}
}
}
}

public static void Main()


{

byte [] MySrcArray = new byte[100];


byte [] MyDstArray = new byte[100];

for(int i = 0; i < MySrcArray.Length; ++i)

MySrcArray[i] = (byte) i;

CopyArray(MySrcArray, MyDstArray);

Console.Read();

}
}

int * Pointer
Loop
Operand Right-Shift Operator >>
Count Operand

Loop
Operand Bitwise AND
Count Operand
Loop Loop
Array
Loop

Pointers

Pointers
Pointers MSDN
Pointers

C++ C C# Pointers
C# Pointers

Reference

c++
Reference

c++ Reference

#include<iostream.h>
include<conio.h>
void main()
{
Clrscr();
int x= 300;
int &ref=x;
cout<<"\ n x= "<<x;
cout<<"\ n p= "<<ref;
ref= 600;
cout<<"\ n x= "<<x;
cout<<"\ n p= "<<ref;

getch;
}

You might also like