Вы находитесь на странице: 1из 55

# Introduction to Pointers

## Every variable has three major item associated with it:

Data Type of the variable Actual Value Stored in the Variable The Address of the Variable

Introduction to Pointers

## Pointers are special type of variables in which a memory address is stored.

They contain memory address and not the value of the variable

Introduction to Pointers

## int x In this case a name x has been associated

with a memory location We can have the memory address of x, say 6000 or whatever it is

Introduction to Pointers
int *p Means a location P which can contain address of an integer In other word P can point towards an integer variable (hence given the name Pointer)

Declaration of Pointers

datatype *X X is name of Pointer Datatype is the type of type to which X will point Each variable that is declared as pointer must be preceded by an *

Example
int x=14, *P; //P is a pointer
P ? x 14

P &x x 14

## Direct vs. Indirect Reference

Normally a variable contains a specific value Pointers contain address of variable So a variable name directly reference a value and a pointer indirectly references a value

Dereference Operator

To get the value stored at the memory address we use dereference operator(*)
* is used with the name of the pointer to get the value stored at the address

Note that address of a and value of ptr are identical & and * are inverses of each other----when they are applied to ptr, consecutively (in any order) the same result is printed.

Initialization of Pointers

Pointers should be initialized either when they are declared or in an assignment statement A pointer may be initialized to 0, NULL or to a address Pointer initialized with 0 or NULL points to nothing

## Arrays are always passed by Ref

Arrays are always passed by Reference Arrays are not passed using & Name of the array is the address of starting location in the memory of the array The name of array is equivalent to &Array

## Pointer Expressions and Pointer Arithmetic

Pointers are valid operands in arithmetic expressions, assignment expressions and comparison expressions However not all the operators used in these expressions are valid for pointer variables A limited set of arithmetic operations may be performed on pointers

A pointer may be incremented or decremented (++ and --) An integer may be added to a pointer(+ or +=) An integer may be subtracted from a pointer(- or -=) One pointer may be subtracted from another

## Arrays are always passed by Ref

Arrays are always passed by Reference Arrays are not passed using & Name of the array is the starting location in the memory of the array The name of array is equivalent to &Array

## Pointer Expressions and Pointer Arithmetic

Pointers are valid operands in arithmetic expressions, assignment expressions and comparison expressions However not all the operators used in these expressions are valid for pointer variables A limited set of arithmetic operations may be performed on pointers

A pointer may be incremented or decremented (++ and --) An integer may be added to a pointer(+ or +=) An integer may be subtracted from a pointer(- or -=) One pointer may be subtracted from another

Pointer Arithmetic
int *y = &x; y = y+1;

The y = y+1 will set y to the address of the next integer after x If x has address 0, then it uses bytes 0, 1, 2, 3. Thus y gets 0, and then changes to 4 (not 1)

## When we declare array as

int y

This means that we have reserved memory spaces for ten integers and named it collectively as y Y represents the memory address of the beginning of this collective memory space

## When we declare array as

int y

The first element of array can be accessed as y Memory address of first element i.e. y is stored in y

## Relationship between Arrays and Pointers

The name of array is a constant pointer which contains the memory address of the first element of the array The difference between this and an ordinary pointer is that the array name is a constant pointer

## Relationship between Arrays and Pointers

The difference between this and an ordinary pointer is that the array name is a constant pointer It means that array name will always point to start of the array It can not be reassigned to any other address

Example
int y; int *yptr; yptr=y; Now we have two things pointing to the same place (y and yptr) Both are pointing towards first element of the array

Example
int y,x; int *yptr=&x; y=yptr;

## Pointer Arithmetic and Expressions

What is an array?

## This is a sequence of 40 bytes four for

each of the 10 integers In truth, arr is of type int*, with the same address as arr

## Since arr is an int*, what is arr+1?

forward) which is arr

## A picture of int arr[x]

0 1 2 3 4 5 6 7

8 9 10 11 12 13 14 15 16 17 18 19

## arr is at address 0 which is equal to arr

arr is at address 4 which is equal to arr+1 arr is at address 8 which is equal to arr+2 arr is at address 12 which is equal to arr+3 arr is at address 16 which is equal to arr+4

Example
int main() { int arr; int i; for (i=0; i < 20; i++) {
*(arr+i) = 20-i; //Assigns values to the array

} for (i=0; i < 20; i++) { cout << arr[i]; // Prints the values }

Explanation

The example prints all numbers from 0 to 19 In the example, we access the elements in two different ways:

In the first loop, we used pointer arithmetic In the second loop, use the [ ] operator They do the same thing we could have
used either method for either loop

Another Example

When a Pointer is incremented it actually jumps the number of memory spaces according to the data type that it points to

void*

## This is useful as a generic type it

allows us to abstract data types We will not go into the detail of what is an ADT

Warning!

You can use the expression ptr++ for a pointer variable But You cannot use the ++ operation if the pointer is the name of an array It is a constant pointer and can't be changed.

## Consider the following:

int* p = 20; *p = 0; Reset bytes 20, 21, 22 and 23, each, to 0. Who knows it could be important!! Maybe it was some other variable you are using Maybe it has something to do with the hard drive It is theoretically possible to crash the system

Declaring Array

## Array size has to be a constant

Need

What if you need to specify array size when program is executing (Runtime)
You will also need this in your project

## Dynamic Memory Management with Operators new and delete

new

Consider
int *Ptr; Ptr = new int;

new operator

Creates variable of proper size for type int Returns pointer of specified type
int *gradesArray = new int[ 10 ];

Allocating arrays

delete

## free space Consider

delete Ptr;

Operator delete

Deallocates memory
De-allocating arrays delete [] gradesArray;

## What about 2-D Array

Pointers to Pointers

We use double dereference to access the elements of a 2D array by using Array name (a pointer) to access a row (another pointer) and further to access a column element In case of single dereference, the value of pointer is the address of the variable that contains the value desired

Double Indirection

## Pointer 1 Address of Variable

Variable Value

Earlier we used arrays and pointers interchangeably We can think a pointer to pointer is like a pointer to a group of arrays because a pointer itself can be considered as an array

Example

Suppose we have a group of character strings We can store them in a 2D array If we use conventional 2D array like Name we can store 5 strings each of length 9 characters.

Arrays of Pointers

## Commonly used to store array of strings

char *suit[ 4 ] = {"Hearts", "Diamonds", "Clubs", "Spades" };

## Each element of suit points to char * (a string)

Array does not store strings, only pointers to strings suit array has fixed size, but can point to strings of any size

Arrays of Pointers

More Examples

void main() { char *Name; int Total; cout<<"How many Names will You Enter"<<endl; cin>>Total; for(int i=0;i<Total;i++) Name[i]=new char; for(i=0;i<Total;i++) { cout<<"Enter" <<i<<"th Name"<<endl; cin>>Name[i]; } for(i=0;i<Total;i++) cout<<Name[i]<<endl; }

Thank You