pointers in c++ - wordpress.com · 6/27/2016 · allowed pointer arithmetic •following are the...

23
Developed By Ms. K.M.Sanghavi Pointers

Upload: trandung

Post on 27-Apr-2019

224 views

Category:

Documents


0 download

TRANSCRIPT

Developed By Ms. K.M.Sanghavi

Pointers

Introduction

• A pointer is a variable which contains the address (in memory) of another variable.

• Pointers are symbolic representation of addresses

• We can have a pointer to any variable type.

Declare

Type of data stored in the

address of the variable.

Indirection operator

Declaring pointer Passing Address

Example

#include <iostream.h>

void main( )

{

int data = 100;

float value = 56.47;

cout << data << &data << endl;

cout << value << &value << endl;

}

Output: 100 FFF4

56.47 FFF0

CSC2110 - Data Structures/Algorithms 10

56.47

100

FFF1

FFF0

FFF2

FFF3

FFF4

FFF5

FFF6

value

data

Pointers. //Demonstration.

#include<conio.h>

#include<iostream.h>

void main()

{

int x ;

float y;

x = 10;

y = 20.0;

cout<<“Value’s of x and y = “<<x<<y;

getch();

}

4001 4002 4003

4004 4005 4006

4007 4008 4009

Memory

Every Block is a memory cell

Cell no’s / Address

10

20.0

4007

x

4007

4002

y

Pointer Operator’s. Address of (Reference) Operator.

Value At Address (Indirection) operator.

void main()

{ int *int_ptr;

float *float_ptr;

int x = 10 ;

float y = 20.0;

cout<<“Value’s of x and y = “<<x<<“ “<<y;

int_ptr = &x;

float_ptr = &y;

cout<<"int_ptr = "<<int_ptr;

cout<<"\n float_ptr = "<<float_ptr;

cout<<"\n value in int_pointer = "<< *int_ptr;

cout<<"\n value in float_ptr = "<< *float_ptr;

getch(); }

&

*

20.0

4002

10

4007

x y

Value at Address.

Address

Name od address

Pointers contd. & Address of (Reference) Operator.

* Value At Address (Indirection) operator.

// Demonstration.

#include <iostream.h>

#include<conio.h>

void main ()

{

int variable ;

int *mypointer;

mypointer = &variable;

*mypointer = 50;

cout << "value in variable is " <<variable<<endl;

getch();

}

Address of variable = mypointer

50 = value at address

Value in variable is 50

50

3722

variable

//Demonstration

#include <iostream.h>

#include<conio.h>

main ()

{

int x=10;

int *y;

int **z;

cout<<"Value of x = "<<x;

cout<<"\nAdd of x = "<<&x;

cout<<"\nValue of x = "<<*(&x);

y=&x;

cout<<"\nAdd of y = "<<&y;

cout<<"\nAdd in y = "<<y;

z=&y;

cout<<"\nAdd of z = "<<&z;

cout<<"\nAdd in z = "<<z;

getch();

}

& Address of (Reference) Operator.

* Value At Address (Indirection) operator.

2554

*y

10

x

2554 5888

5888

**z

6550

Value of x = 10

Add of x = 2554

Value of x = 10

Add of y = 5888

Add in y = 2554

Add of y = 6550

Add in z = 5888

//Demonstration

#include <iostream.h>

#include<conio.h>

main ()

{

int x=10;

int *y;

int **z;

cout<<"Value of x = "<<x;

cout<<"\nAdd of x = "<<&x;

y=&x;

cout<<“Accessing x via ptr y = ”<<*y;

z=&y;

cout<<“Accessing x via ptr z ”<<**z;

getch();

}

& Address of (Reference) Operator.

* Value At Address (Indirection) operator.

2554

*y

10

x

2554 5888

5888

**z

6550

Value of x = 10

Add of x = 2554

x via prt y = 10

x via prt z = 10

Facts

1. *& variable = variable

2. *(&)variable = *&variable

Example 1 :

int x =10;

cout<<*&x;

cout<<*(&)x;

Example 2 :

int x = 10;

int * p;

p = &x;

cout<<* p;

& Address of (Reference) Operator.

* Value At Address (Indirection) operator.

10

10

Operations on Pointer Variables

• Assignment – the value of one pointer variable can be assigned to another pointer variable of the same type

• Relational operations - two pointer variables of the same type can be compared for equality, and so on

• Some limited arithmetic operations

– integer values can be added to and subtracted from a pointer variable

– value of one pointer variable can be subtracted from another pointer variable

17

Example: Pointer Arithmetic

#include <iostream.h> void main() { int* p1; // simple way of defining pointers int* p2; int* p3; int x; p2 = &x; p1 = p2; p2 = p2 + 5; cout << (p2 - p1);// prints the number of elements

// between the two pointers, which is 5 // p3 = p2 + p1; is illegal operation }

Allowed Pointer Arithmetic

• Following are the allowed pointer operations:

– Subtraction of pointers results in the number of elements between the pointers

– Addition of integral values in pointers

• Add or subtract pointers with an integral value

• Adding two pointers is not allowed

1.4 Dynamic memory management

• new operator allocates memory and returns a pointer to the newly created object.

• When an object, that is allocated by new, is no longer referenced, an operator delete must be applied to the object, through its pointer, to avoid “memory leakage”

Cont.

• Example:

string *str_ptr;

str_ptr = new string("Hello");

cout<< *str_ptr << endl; //Hellow

cout<< (*str_ptr).length() <<endl; //5

delete str_ptr;