pointers a pointer is a variable that contains a memory address as it’s value. the memory address...
TRANSCRIPT
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.
Pointers
• Pointer declarations:<data type> *<variable name>;
• Pointer initialization:*<variable name> = 0; // 0 == NULL
*<variable name> = <variable name2>;
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
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;
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.
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>) { … }
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.
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.
const Qualifier
• A const pointer to const data always points to the same memory location.– The data it points to can not be changed.
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
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>);
Size of Arrays
double arrayOfNums[20];
double numInArray = sizeof arrayOfNums / sizeof( double);
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.
Pointer Arithmetic
int values[5];
int *valuePtr = v;
valuePtr++;
valuePtr++;
valuePtr--;2000 2004 2008 2012 2016
Pointer Arithmetic
• Pointer variables that point to the same array can be added and subtracted.– Need to ensure the result is valid.
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.
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.
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”};
Arrays of Pointers
• Using arrays of pointers allows a program to contain elements that vary in size.– This saves memory.
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
Function Pointers
• It is possible to have an array of function pointers. – This is useful for menu driven systems.– Example – Figure 5.28
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.
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;”
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.
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[]?
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;
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’.
String Manipulation
• C++ provides a sting manipulation library.– Some of the functions are listed on page
346 – 347.– String program.