Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 reference to data. –Pointers must be defined before they can be used.

2 Pointers Pointer declarations: * ; Pointer initialization: * = 0; // 0 == NULL * = ;

3 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

4 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;

5 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.

6 Function Calls using Pointers The function call must send the address of the variable. functionName( & ); The function definition must use an indirection operator to create an alias for the variable. functionName ( * ) { … }

7 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.

8 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.

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

10 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

11 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( );

12 Size of Arrays double arrayOfNums[20]; double numInArray = sizeof arrayOfNums / sizeof( double);

13 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.

14 Pointer Arithmetic int values[5]; int *valuePtr = v; valuePtr++; valuePtr--; 20002004200820122016

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

16 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.

17 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.

18 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”};

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

20 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

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

22 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.

23 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;”

24 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.

25 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[]?

26 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;

27 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’.

28 String Manipulation C++ provides a sting manipulation library. –Some of the functions are listed on page 346 – 347. –String program.


Download ppt "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."

Similar presentations


Ads by Google