pointer in c++

29
POINTERS IN C++ POINTERS IN C++

Upload: suraj-maurya

Post on 08-Apr-2017

86 views

Category:

Education


3 download

TRANSCRIPT

Page 1: Pointer in C++

POINTERS IN C++

POINTERS IN C++

Page 2: Pointer in C++

Present by -

Suraj R. Maurya

Page 3: Pointer in C++

Topics : All About Pointers :

DefinitionDeclaring Pointer Variable&(‘Address of’ Operator)*(‘Value of’ or Indirection Operator)Pointers TypePointer Arithmetic Pass by Pointer in function

Page 4: Pointer in C++

Array of PointerString of PointerObject of Pointerthis PointerAdvantages of PointerLimitations of Pointer

Page 5: Pointer in C++

100… … 1024 … Memory address: 1024 1032 1020

A Variable a’s value, i.e., 100, is stored at memory location 1024

Computer Memory

• Each variable is assigned a memory slot (the size depends on the data type) and the variable’s data is stored there

• Eg: int a=100;

Page 6: Pointer in C++

• Every byte in computer memory has an address.

• Addresses are numbers just like house numbers.

• Addresses start from 0 and go up as 1, 2, 3….e.g. in 640 KB memory addresses will be 0 to 655359 I.e., (640 X 1024)-1.

• The moment a variable is declared, memory is allocated in the RAM for its storage. int X=100;.

• A variable in the RAM can be accessed only if you know its address.

Page 7: Pointer in C++

Definition:

A pointer is a variable which store the memory address of another variable.

It supports dynamic memory allocation routines.

A pointer variable and normal variable is always same data type.

Page 8: Pointer in C++

Declaration And Initialization Of Pointer Variable

• Syntax : • Datatype *variable_name; • eg. int *x; float *y; char *z;

• Address of operator(&)- it is a unary operator that returns the memory address of its operand. Here the operand is a normal variable.

• Ptr_name=&variable_name;

Page 9: Pointer in C++

• eg. int x = 10; int *ptr ; ptr= &x; • Now ptr will contain address where the variable x is stored in memory 10

0x5f0fff2

ptr

x

Address of x

Page 10: Pointer in C++

“*”(‘Value of’ or Indirection Operator)

It is a unary operator that returns the value stored at the address pointed to by the pointer.

Here the operand is a pointer variable.• eg.• int x = 10; • int *ptr = &x; • cout<< ptr;// address stored at ptr will be displayed• cout<<*ptr;// value pointed to by ptr will be

displayed• Now ptr can also be used to change/display the value

of x.

Page 11: Pointer in C++

Pointers Type

Page 12: Pointer in C++
Page 13: Pointer in C++

Pointer Arithmetic

• Two arithmetic operations, addition and subtraction, may be performed on pointers. When we add 1 to a pointer, we are actually adding the size of data type in bytes, the pointer is pointing at.

• For e.g. int *x; x++;• If current address of x is 1000, then x++ statement will

increase x by 2(size of int data type) and makes it 1002, not 1001.

Page 14: Pointer in C++

• Consider p1 and p2 as pointers: P1=&a;p2=&b;• The arithmethic operations that can be

performed on p1 and p2 are summarized in the table below:

Page 15: Pointer in C++

Pointer Expressions:

• A Pointer expression contains pointer variable as an operand and arithmetic operator such as +,-,*,/• Eg: (*p1)++ : This statement increments value,

stored at the memory address pointed by pointer p1 by 1.

Page 16: Pointer in C++

Pass by Pointer in function Consider the program of swapping two variables with the help of pointers: #include<iostream.h> void swap(int *m, int *n) { int temp; temp = *m; *m = *n; *n = temp; } void main() { int a = 5, b = 6; cout << “\n Value of a :” << a << “ and b :” << b; swap(&a, &b); cout << “\n After swapping value of a :” << a << “and b :” << b; }

Page 17: Pointer in C++

COMPARING PASS BY VALUE, PASS BY

REFERENCE AND PASS BY POINTER

PASS BY VALUE PASS BY REFERENCE PASS BY POINTERSeparate memory is allocated to formal parameters.

Formal and actual parameters share the same memory space.

Formal parameters contain the address of actual parameters.

Changes done in formal parameters are not reflected in actual parameters.

Changes done in formal parameters are reflected in actual parameters.

Changes done in formal parameters are reflected in actual parameters.

For eg. void cube(int x){ x= x*x*x; }void main(){int y=10;cout<<y<<endl;cube(y); cout<<y<<endl;}output:1010

For eg.void cube(int &x){ x= x*x*x; }void main(){int y=10;cout<<y<<endl;cube(y); cout<<y<<endl;}output:101000

For eg.void cube(int *x){ *x= (*x)*(*x)*(*x); }void main(){int y=10;cout<<y<<endl;cube(&y); cout<<y<<endl;}output:101000

Page 18: Pointer in C++

Array of Pointer• Array is collection of similar data type.• We can create pointer variable with array both are

similar data type.• Using array of pointer searching, insertion, deletion etc.

operation we can perform.• Syntax: Data_type variable_name[size of array]; Data_type *pointer_variable; pointer_variable=variable_name; OR pointer_variable=&variable_name[0];

Page 19: Pointer in C++

• Example of pointer of array: Void main(){ int a[2],*ptr;Ptr=&a[0];Cout<<“Ptr :”<<ptr;*Ptr++;}

Page 20: Pointer in C++

String of Pointer

• Array of character is called string.• We can create pointer variable with string both are

similar data type.• Using string of pointer searching, finding length ,

comparisons ,concatenation ,reverse etc operation we can perform.

• Syntax: Data_type variable_name[size of string]; Data_type *pointer_variable; Pointer_variable=&variable_name[0];

Page 21: Pointer in C++

• Example of string of pointer: void main(){Char name[4]=“NEEL”,*ptr;Ptr=&name[0];While(*ptr!=‘\0’){Ptr++;Cout<<*ptr;}}

Page 22: Pointer in C++

Object of Pointer

• When address of an object of a class is stored into the pointer variable of the same class type then it is pointer of object.

• This pointer can be used to access the data member and member function of same class.

Page 23: Pointer in C++

Syntax:

• Class_name object_name;• Class_name *pointer_variable;• Pointer_variable=&object_name;• Pointer_variable->function_name();

Page 24: Pointer in C++

Example of pointer to object:

• //create a class syco;• //and declare data member and function;• //Now, in main function:Syco obj;Syco *ptr;Ptr=&obj;Obj->getdtat();Obj->putdata();

Page 25: Pointer in C++

this Pointer• C++ uses a unique keyword called “this” to

represent an object that invokes a member function.

• This is a pointer that points to the object for which this function was called.

• This is a unique pointer is automatically passed to a member function when it is called.

• The pointer this acts as an implicit argument to all the member function.

• its an in-built pointer so programmer doesn’t need to explicitly create this pointer.

Page 26: Pointer in C++

• One class can have exactly one this pointer.• Syntax: There are two ways of accessing member with this pointer as follows: this->data_member; this->member_function(); OR *this(dtat_member);

Page 27: Pointer in C++

Example of this pointer:Class abc{ Int a;Public:Void getdata(){}};The private variable ‘a’ can be used directly inside a member function, like a=123;But in this concept(in main function) to do same job:This->a=123;This ->getdata();

Page 28: Pointer in C++

Advantages of Pointer

I. Pointer can handle array more efficiently.II. As they provide direct access to memory, they process

data very fast.III. They reduce storage space.IV. They give alternate way to access elements of an

array.V. Pointers avoid any confusion by the compiler due to

variables with same name.VI. They help in building complex data structures like

linked list , stacks , queues , trees , graphs etc.

Page 29: Pointer in C++

Limitations of Pointer

i. They are slower than normal variables.ii. Pointers store only address but cannot store

a value.iii. It requires a null pointer reference.iv. If a pointer is initialized with incorrect value ,

it can lead to a garbage value which may cause error in the program.