pointers in c++

22
POINTERS IN C++ BY - SAI TARLEKAR

Upload: sai-tarlekar

Post on 08-Apr-2017

73 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Pointers in c++

POINTERS IN C++BY - SAI TARLEKAR

Page 2: Pointers in c++

Points Introduction. Declaration and initialization of pointers. Small program. Thumb rule. Pointer Variable. Pointer Arithmetic. Reference Variable. Reference as function arguments

Page 3: Pointers in c++

What is a Pointer???

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

This memory address is the location of another variable where it has been stored as memory.

Therefore if a variable contains the address of another variable is said to point to the second.

Page 4: Pointers in c++

Declaration and Initialization of Pointers Syntax : Datatype *variable_name;

eg. int *x; float *y; char *z;

eg. int x = 10; int *ptr = &x;

Now ptr will contain address where the variable x is stored in memory.

Page 5: Pointers in c++

int *ptr;

This tells the compiler three things about ptr:-1) The (*) tells that the variable ptr is a pointer

variable. 2) Pointers need a memory location.3) Pointers points to a variable of type int.

Page 6: Pointers in c++

Program

Page 7: Pointers in c++

Output:-

Page 8: Pointers in c++
Page 9: Pointers in c++

More info….1) A pointer that is not initialzed is called as wild pointer

because you have no idea what it is pointing to.

2) A pointer whose value is zero is called as null pointer.

3) Void pointer - void pointer is a special type of pointer that can be pointed at objects of any data type. A void pointer is declared using data type void.

Page 10: Pointers in c++

Thumb rule of pointers:-

1) Type must be same to the variable whose address is going to pass to the pointer.

2) When you want to pass the address of the variable use ‘&’ before the variable name.

3) When you want to pass the value of the variable use ‘*’ before the pointer.

Page 11: Pointers in c++

Pointer variableConsists of two parts

Pointer operator

(*) asteriskAddress operator

(&) ampersand

Page 12: Pointers in c++

Pointer operator A pointer operator or a pointer variable is represented by a

combination of asterisk(*) with a variable name.Syntax:-

data_type *pointer_variable;

eg. If a variable of character data type and also declared as * (asterisk) with another variable it means the variable is of type “Pointer to character”. char *ptr;

where ptr is a pointer variable which holds the address of an character data type.

Page 13: Pointers in c++

Address Operator

An address operator can be represented by a combination of and (ampersand) with a pointer variable.

The and is a unary operator that returns the memory address of its operand.

Syntax:- data_type variable1; data_type variable=&variable1;

Eg:- int x; int *ptr=&x;It means that ptr receives the address of x;

Page 14: Pointers 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.

Eg. 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 15: Pointers in c++

Data is stored in contiguous memory cell.

The Number of memory cells required to store a data item depends on the type of data item

Char 1 byte

Integer 2 byte

Floating 4 byte

Double 8 byte

Page 16: Pointers in c++

Program

Page 17: Pointers in c++

Output:-

Page 18: Pointers in c++

REFERENCE VARIABLEA reference variable is a name that acts as an alias or an alternative name, for an already existing variable.

Syntax:-Data type &variable name = already existing variable;

Eg. int num=10;int & sum = num; // sum is a reference variable or

alias name for num

NOTE: Both num and sum refer to the same memory location. Any changes made to the sum will also be reflected in num.

10sum

num

Page 19: Pointers in c++

void cube(int &x){ x= x*x*x; }void main(){int y=10;cout<<y<<endl;cube(y);cout<<y<<endl;}

x

In the above program reference of y is passed. No separate memory is allocated to x and it will share the same memory as y. Thus changes made to x will also be reflected in y.

OUTPUT:101000

REFERENCE AS FUNCTION ARGUMENTS

y

100

Page 20: Pointers in c++

Program#include<iostream.h>#include<conio.h>swap(int *,int *)void main(){int a,b;cout<<"\nEnter value for A : ";cin>>a;cout<<"\nEnter value for B : ";cin>>b;cout<<"\n\nBefore Swapping : ";cout<<"\n\t\t\ta = "<<a;cout<<"\n\t\t\tb = "<<b;swap(&a,&b);

cout<<"\n\nAfter Swapping : ";cout<<"\n\t\t\ta = "<<a;cout<<"\n\t\t\tb = "<<b;getch();}swap(int *x,int *y){int temp;temp=*x;*x=*y;*y=temp;}

Page 21: Pointers in c++

Output:-

Page 22: Pointers in c++

Thank you