Top Banner
pointer • What it is • How to declare it • How to use it • Relationship between arrays and pointers • Relationship between strings and pointers
26

Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

pointer

• What it is

• How to declare it

• How to use it

• Relationship between arrays and pointers

• Relationship between strings and pointers

Page 2: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

pointerA pointer is a variable that contains a memory address as its value

count

10

countPtr

Page 3: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

pointer

count

countPtr

memory address 1000 10

1000

count

10

countPtr

1000

Page 4: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

How to declare a pointer

int * countPtr;

countPtr is a variable of type “pointer to int”

Page 5: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

example

count

countPtr

memory address 100010

1000

int count = 10;int *countPtr = 1000;

Page 6: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

How to initialize a pointer to point to an address

count

countPtr

memory address 100010

1000

int count = 10; int * countPtr = &count;

& is an operator that returns the address of a variable.

Page 7: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

Restrictions for using & operator• May be used for variables

int count;

int *countPtr = &count;

• May not be used with constants (&10)

Page 8: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

How to initialize a pointer to point to nothing

int * aPtr = NULL;

OR

int * aPtr; aPtr = NULL;

Page 9: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

Indirect and direct references

Direct reference – changing count memory location using count variable

Indirect reference – changing count memory location using countPtr variable

count

10

countPtr

Page 10: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

Indirect and direct references

int count; int * countPtr = &count;

count = 12; // direct reference*countPtr = 12 // indirect reference

* is the indirection operator. Means change the memory location currently pointed to by countPtr.

count

10

countPtr

12

Page 11: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

Exercises

1) Declare an integer variable called x. Declare a pointer to x called xPtr and initialize it so that it points to the variable x. Use xPtr to indirectly change the value of x to 20;

2) Declare a second integer variable called y. Declare a pointer to y called yPtr and initialize it so that it points to the variable y. Calculate the sum of x + y using xPtr and yPtr.

Page 12: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

pointer arithmetic

int *aPtr;aPtr++; // is valid but what does it do?

integer (4 bytes) aPtr

aPtr is incremented by the size of whatever it is pointing to. In this example 4 bytes. So the value in aPtr is increased by 4 by the aPtr ++ statement

Page 13: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

pointer arithmetic

char *aPtr;aPtr++;

char (1 byte) aPtr

aPtr is incremented by the size of whatever it is pointing to. In this example 1 byte. So the value in aPtr is increased by 1 by the aPtr ++ statement

Page 14: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

using pointer arithmetic

grades[0]grades[1]grades[2]grades[3]grades[4]

10085506588

int gradesPtr = &grades[0];

gradePtr

gradesPtr++; gradePtr

Page 15: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

using pointer arithmetic

letters[0]letters[1]letters[2]letters[3]letters[4]

‘a’‘b’‘c’‘d’‘e’

char letterPtr = &letters[0];

letterPtr

letterPtr ++; letterPtr

Page 16: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

pointers and arrays

• array name is a pointer. This is why:can pass array name to a function without the

bracketsarrays are passed to functions as reference

parameters by default

Page 17: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

arrays as function parametersPass the name of the array without the brackets

void PrintResults(int results[ ]); // function prototype

void PrintResults(int results[ ]) // function definition{

}

calling sequence: const int maxRange = 10; int frequency[maxRange ] = {0}; PrintResults (frequency);

Page 18: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

pointers and arrays

int b[ ] = {1,2,3,4,5};

b[0]b[1]b[2]b[3]b[4]

12345

b

Page 19: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

pointers and arrays

int *bPtr; int b[ ] = {1,2,3,4,5};

bPtr = &b[0]; or bPtr = b;

b[0]b[1]b[2]b[3]b[4]

12345

bPtr

b

Page 20: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

pointers and strings

char digits [ ] = {“0123456789”};

‘0’‘1’

‘3’‘4’‘5’‘6’‘7’‘8’‘9’

digits[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

‘2’

NULL

Page 21: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

pointers and strings

char digits [ ] = {“0123456789”};

‘0’‘1’

‘3’‘4’‘5’‘6’‘7’‘8’‘9’

digits[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

‘2’

NULL

char *digitsPtr = digits;

digitsPtr

Page 22: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

simplify

Instead of char digits [ ] = {“0123456789”};

char *digitsPtr = digits;

do char *digitsPtr = “0123456789”;

Page 23: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

simplify

char *digitsPtr = “0123456789”;

cout << digitsPtr << endl; // displays all the numbers in the string

Page 24: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

Simplify Array of strings

const int Size = 5; const int WordSize = 10;

char article [Size] [WordSize] = {"the", "a", "one", "some", "any"}; char noun [Size] [WordSize] = {"boy", "girl", "dog", "town", "car"}; char verb [Size] [WordSize] = {"drove", "jumped", "ran", "walked", "skipped"}; char preposition [Size] [WordSize] = {"to", "from", "over", "under", "on"};

Page 25: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

Array of strings

const int Size = 5; char *article [Size] = {"the", "a", "one", "some", "any"}; char *noun [Size] = {"boy", "girl", "dog", "town", "car"}; char *verb [Size] = {"drove", "jumped", "ran", "walked", "skipped"}; char *preposition [Size] = {"to", "from", "over", "under", "on"};

Page 26: Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.

The End !!!!