pointers a pointer is a variable that contains a memory address as it’s value. the memory address...

28
Pointers •A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. – A pointer is an indirect reference to data. – Pointers must be defined before they can be used.

Upload: patrick-nichols

Post on 14-Jan-2016

239 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Pointers

• A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data.– A pointer is an indirect reference to data.– Pointers must be defined before they can

be used.

Page 2: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Pointers

• Pointer declarations:<data type> *<variable name>;

• Pointer initialization:*<variable name> = 0; // 0 == NULL

*<variable name> = <variable name2>;

Page 3: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Address Operator

• The & is applied to the pointer variable to access the operand address.

char let = ‘A’;

char *letter;

letter = &let; // The same as *letter = let

Page 4: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Dereferencing

• Dereferencing a pointer occurs when the * operator is used to access the data stored in the pointer variable.cout << *letter << endl;

– The above is the same as:cout << let << endl;

Page 5: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Function Calls using Pointers

• Function calls using pointer arguments are call-by-reference.– Passing pointers to functions saves large

data structures from being passed to the function.

• Remember call-by-reference implies that the original data can be modified.

Page 6: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Function Calls using Pointers

• The function call must send the address of the variable.functionName( &<variable name>);

• The function definition must use an indirection operator to create an alias for the variable.<return type> functionName (<data type> *<variable

name>) { … }

Page 7: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

const Qualifier

• The const qualifier defines a value to be a constant. – Any value that is passed to a function and

should not be modified by the function must be defined as const to ensure it is not modified.

Page 8: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

const Qualifier• A non-constant pointer to const data is a

pointer that can be modified to point to any data item of the same data type.– The actual data it points to can not be changed.

• A const pointer to non-constant data always points to the same memory location.– The data it points to can be modified.

Page 9: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

const Qualifier

• A const pointer to const data always points to the same memory location.– The data it points to can not be changed.

Page 10: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Passing Arrays

• When an array is passed to a function, to actual data sent is the address of the first element. void myFunction (int array[]) {…}

void myFunction (int *array) {…}

– Example Figure 5.15

Page 11: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Size of Arrays

• C++ provides the sizeof function that can be used to derive the number of elements in an array.– sizeof is a compile time operator and does

not affect run-time execution.

int numElements = sizeof arrayName / sizeof(<arrayName data type>);

Page 12: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Size of Arrays

double arrayOfNums[20];

double numInArray = sizeof arrayOfNums / sizeof( double);

Page 13: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Pointer Arithmetic

• Pointers can be incremented, or decremented.– The increment and decrement operators

can produce different results based upon the data type of the pointer.

• The pointer will not necessarily be incremented/decremented by 1.

Page 14: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Pointer Arithmetic

int values[5];

int *valuePtr = v;

valuePtr++;

valuePtr++;

valuePtr--;2000 2004 2008 2012 2016

Page 15: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Pointer Arithmetic

• Pointer variables that point to the same array can be added and subtracted.– Need to ensure the result is valid.

Page 16: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Pointer Arithmetic

• A pointer can be assigned to another pointer only if they both have the same data type.– The only exception is a void pointer that

can be used to represent a pointer of any data type.

• A void pointer has to first be cast to the proper data type before it can be assigned a pointer of that data type.

Page 17: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Pointer Comparison

• Pointer comparisons compare the memory address of the pointers.– This is only useful for arrays.

• You should use array notation for manipulating arrays.

Page 18: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Arrays of Pointers

• A multi-dimensional array is essentially an array of pointers.

• An array of strings is also an array of pointers.– The elements of the array point to the first

character in the string.

char *arrayName[4] = {“string 1”, “string 2”,

“string 3”, “string 4”};

Page 19: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Arrays of Pointers

• Using arrays of pointers allows a program to contain elements that vary in size.– This saves memory.

Page 20: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Function Pointers

• Pointers can be used to store the address of a function in memory.– Such pointers can be passed to functions,

returned from functions, stored in arrays, and assigned to other function pointers.

– Example program – Figure 5.26

Page 21: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Function Pointers

• It is possible to have an array of function pointers. – This is useful for menu driven systems.– Example – Figure 5.28

Page 22: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Characters

• Characters in C++ include the ASCII code and individual characters are also represented as integers based upon the ASCII code.– Characters are surrounded by single quotes.

• ‘a’, ‘\n’

– A char variable represents either a single character or an array of characters.

Page 23: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Strings

• A string is a series of characters that are perceived as a single unit.– Strings are surrounded by double quotes.– Examples:

“It’s Sunday, watch football!!”

“x = 1 + y;”

Page 24: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Strings

• C++ represents strings as an array of characters that includes the null character (‘\0’) at the end.– The value of a string is the address of the

first character.

Page 25: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Defining Strings

• Essentially strings are arrays:char name[] = “Mary”;

• Just like arrays, strings can also be represented as pointers:char *namePtr = “Mary”;

– How many characters are stored in name[]?

Page 26: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Inputting Strings

• The cin functionality can be used to read in a string.cin >> aString;

• The above statement will only read characters until a space, tab, new line, or eof are encountered.

– To ensure that the string does not exceed the length of the array:

cin >> setw(lengthOfArray - 1) >> aString;

Page 27: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

Inputting Strings

• An entire line of text may also be input using the getline function.cin.getline(arrayName, numberOfCharacters,

delimiterCharacter);

– getline will stop reading in characters when the delimiter character is reached, the eof character is entered, or the total number of elements has been read.

– The default delimiter character is ‘\n’.

Page 28: Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect

String Manipulation

• C++ provides a sting manipulation library.– Some of the functions are listed on page

346 – 347.– String program.