Chapter 9 - Pointers

¡Supera tus tareas y exámenes ahora con Quizwiz!

9.7 What is happening in this statement? sum += *arr;

The * operator will first dereference arr, then the ++ operator will increment the address in arr. p 513

9.6 The capability of comparing addresses gives you another way to

be sure a pointer does not go beyond the boundaries of an array. p 508

9.1 Getting the address of a variable is accomplished with an _______ in C++.

operator p 492

9.5 Pointer variables may be defined in the same statement as

other variables of the same type p 506

9.7 Pointer variables can also be used to accept...

array addresses as arguments. p 512

9.3 Pointers can be used as

array names. p 500

9.1 What does the address operator & do?

& returns the memory address of a variable. p. 491

9.1 The address is of a generic variable is stored in

Hexadecimal. This is the way addresses are normally shown in C++ p 493

9.7 In a program using the C language (not C++) the only way to pass a variable by reference is to use

a pointer 509

9.3 An element in an array can be retrieved by

using its subscript or by adding its subscript to a pointer to the array. *(numbers + 0) retrieves the first element in the array *(numbers + 1) retrieves the second element in the array and so on. p 501

9.2 Recall from chapter 6 that when we pass an array as an argument in a function

we are actually passing the arrays beginning address. p 493

9.2 When we apply the indirection operator (*) to ptr

we are working with the item that ptr points to. p 499

9.2 Some programmers prefer to define pointers

with an asterisk next to the type name, rather than the variable name. int* ptr; p 496

9.6 Comparing two arrays is not the same as

comparing the two values the two pointers point to. p 508

9.7 When useing pointer variables, either _______ or ______ _______ may be used to work with the contents of an array.

subscript, pointer notation p 512

9.9 When writing functions that return pointers...

take care not to create elusive bugs. p 522

9.8 A more practical use of the new operator is..

to dynamically create an array. p 519

9.2 What does dereference mean?

When the indirection operator is placed in front of a pointer variable name, it dereferences the pointer. p 497

9.7 What must happen when you pass the address of a const item into a pointer?

When this is the case, the pointer must be defined as a pointer to a const item. p 513

9.9 What is wrong with this code? string *getFullName() { string fullName[3]; cout << "Enter your first name: "; getline(cin, fullName[0]); cout << "Enter your middle name: "; getline(cin, fullName[1]); cout << "Enter your last name: "; getline(cin, fullName[2]); return fullName; }

When writing functions that return pointers you should take care not to create elusive bugs. The problem is that the function returns a pointer to an array that no longer exists. Because the fullName array is defined locally, it is destroyed when the function terminates. Attempting to use the pointer will result in erroneous results. p 522

9.2 What is dynamic memory allocation?

When you are writing a program that will need to work with an unknown amount of data, dynamic memory allows you to create variables, arrays and more complex data structures in memory while the program is running. p 495

9.7 A reference variable acts as an...

alias to the original variable used as an argument. This gives the function access to the original argument variable, allowing it to change the variables contents. p 509

9.8 Dynamic memory allocation..

allows a program to create its own variables, through the use of pointers. p 518

9.2 Remember: Integers can only hold one kind of value

an address p 496

9.3 The address operator is not needed when

an array's address is assigned to a pointer. p 503

9.5 A pointer can only be initialized with the address of

an object that has already been defined. p 506

9.4 A pointer may be subtracted from

another pointer. p 505

9.3 When working with arrays remember the following rule:

array[index] == *(array + index) p 502

9.2 Recall from ch 6 that a reference variable acts

as an alias for another variable. It is called a reference variable because it references another variable in the program. Anything that you do to the reference variable is done to the actual variable it references. p 494

9.6 C++'s relational operators may be used to

compare pointer values. If one address comes before another address in memory, the first address is considered "less than" the second. p 507

9.3 Array names can be used as

constant pointers. p 500

9.8 Variables may be _________ and _________ while a program is running.

created, destroyed p 518

9.2 In object oriented programming pointers are very useful for

creating and working with objects and for sharing access to those objects. p 495

9.8 What is happening in this code? delete iptr;

delete is being used to free a single variable, being pointed to by iptr. p 520

9.9 Like any other data type...

functions may return pointers. p 522

9.3 You can use the address operator to

get the address of an individual element in an array. p 503

9.10 An array of pointers can be used to sort the contents of a second array...

in sorted order, without sorting the second array. p 529

9.2 The definition of a pointer variable looks like

int *ptr; p 496

9.3 The _________ are critical when adding values to pointers.

parenthesis. *numbers + 1 != *(numbers + 1); The first adds 1 to the contents of the first element in the array. The second adds 1 to the address in number, then dereferences it. p 501

9.7 What is happening in this code? const int SIZE = 6; const double payRates[SIZE] = { 18.55, 17.45, 12.85, 14.97, 10.35, 18.89 };

payRates is an array of const doubles. This means that each element in the array is a const double and the compiler will not allow us to write code that changes the array's contents. If we want to pass the payRates array into a pointer parameter, the parameter must be declared, as a pointer to cost double. p 514

9.2 Pointers...

point to some piece of data that is stored in the computer's memory. p 493

9.4 You cannot multiply or divide a

pointer p 505

9.3 Array names are

pointer constants. You can't make them point to anything but the array they represent. p 504

9.6 Pointers may be compared by using any of C++'s

relational operators. p 507

9.5 A pointer is designed to point at an object of a

specific data type p 506

9.2 Memory addresses identify

specific locations in the computer's memory. p 493

9.7 Constant pointers must be initialized with a _______ ______ .

starting value. p 516

9.8 To dynamically allocate memory means...

that a program, while running, asks the computer to set aside a chunk of unused memory large enough to hold a variable of a specific data type. p 518

9.2 The int data type indicates

that ptr can be used to hold the address of an integer variable. p 496

9.2 The asterisk in front of the variable name indicates

that ptr is a pointer variable. p 496

9.2 Int does not mean

that ptr is an integer variable. It means that ptr can hold the address of an integer variable. p 496

9.3 The only difference between array names and pointer variables is

that you cannot change the address an array name points to. p 504

9.7 A pointer to const can also recieve...

the address of a nonconstant item. p 514

9.5 Pointers may be initialized with

the address of an existing object. p 506

9.7 If a constant pointer is used as a function parameter...

the parameter will be initialized with the address that is passed as an argument into it, and cannot be changed to point to anything else while the function is executing. p 516

9.7 In passing the address of a constant into a pointer variable, ...

the variable must be defined as a pointer to a constant. If the word const had been left out of the parameter definition, a compiler error would have resulted. p 514

9.2 The real benefit of pointers is that

they allow you to indirectly access and modify the variables being pointed to. p 497

9.8 What is the delete operator used for?

The delete operator is used to free memory that was allocated with new. p 520

9.7 What is happening in this statement? const double *rates

*rates is pointing to some address of type double. Because rates is a pointer to a const, the compiler will not allow us to write code that changes the thing that rates points to. The asterisk next to rates (*rates) indicates that rates is a pointer. p 514

9.9 You should return a pointer from a function only if it is:

- A pointer to an item that was passed into the function as an argument. - A pointer to a dynamically allocated chunk of memory. p 523

9.7 What is happening in this statement? const int * const ptr

- const int is what ptr points to - * const ptr - const indicates that ptr is a constant pointer p 517

9.7 What is happening in this statement? int * const ptr

- int is what ptr points to - * const indicates that ptr is a constant pointer. p 516

9.7 When a variable is passed into a reference parameter...

... The argument is said to be passed by reference. p 509

9.1 Each byte of memory has a unique...

... address p. 491

9.8 What is a NULL pointer?

A pointer that contains the address 0 is called a NULL pointer. p 520

9.7 What is the difference between a pointer to const and a const pointer?

A pointer to const points to a constant item. The data that the pointer points to cannot change, but the pointer itself can change. With a const pointer, it is the pointer itself that is constant. Once the pointer is initialized with an address, it cannot point to anything else. p 516

9.2 What is an indirection operator?

An indirection operator is the asterisk (*) which occurs around a pointer. int *ptr; p 497

9.2 What are three different uses of the asterisk, we've used so far in C++;

As the multiplication operator, in statements such as, distance = speed * time; In the definition of a pointer variable, such as int *ptr; As the indirection operator, in statements such as *ptr = 100; p 500

9.7 What is happening in this code? int value = 22; int * const ptr = &value;

In the definition of ptr, the word const appears after the asterisk. This means that ptr is a constant pointer and can only point to one thing. It points to the address of value. p 516

9.7 What is happening in this code? void displayPayRates(const double *rates, int size) { // Set numeric output formatting cout << setprecision(2) << fixed << showpoint; // Display all the pay rates. for (int count = 0; count < size; count++) { cout << "Pay rate for employee " << (count + 1) << " is $" << *(rates + count) << endl; } }

In the function header, notice that the rates parameter is defined as a pointer to const double. It should be noted that the word const is applied to the thing that rates point to, not rates itself. p. 514

9.7 What is happening in this code? void displayValues(const int * const numbers, int size) { // Display all the values. for (int count = 0; count < size; count++) cout << *(numbers + count) << " "; cout << endl; }

In this code, the parameter numbers is a const pointer to a const int. Although we can call the function with different arguments, the function itself cannot change what numbers points to, and it cannot use numbers to change the contents of an argument. p 517

9.8 A program created with an older compiler should always check to see if the new operator returns...

NULL p 519

9.7 Pointers to const do what?

Point to const data. p 516

9.2 What are pointer variables?

Pointer variables, which are often just called, pointers, are designed to hold memory addresses. With pointer variables you can indirectly manipulate data stored in other variables. p 493

9.8 What is happening in this code? iptr = new int

The statement is requesting that the computer allocate enough memory for a new int variable. The operand of the new operator is the data type of the variable being created. Once the statement executes, iptr will contain the address of the newly allocated memory. p 518

9.9 What is happening in this code? char *findNull(char *str) { char *ptr = dtr; while (*ptr != '\0') ptr++; }

This code locates the null terminator which appears at the end of a string (such as a string literal) and returns a pointer to it. The char * return type in the function header indicates the function returns a pointer to a char. p 522

9.9 What is happening in this code? string *getFullName(string fullName[ ]) { cout << "Enter your first name: "; getline(cin, fullName[0]); cout << "Enter you middle name: "; getline(cin, fullName[1]); cout << "Enter your last name: "; getline(cin, fullName[2]); return fullName; }

This function accepts a pointer to the memory location where the user's input is to be stored. Because the pointer references a memory location that was valid prior to the function being called, it is safe to return a pointer to the same location. p 523

9.9 What is happening in this code? string *getFullName() { string *fullName; fullName = new string[3]; cout << "Enter your first name: "; getline(cin, fullName[0]); cout << "Enter your middle name: "; getline(cin, fullName[1]); cout << "Enter your last name: "; getline(cin, fullName[2]); return fullName; }

This function uses the new operator to allocate a section of memory. This memory will remain allocated until the delete operator is used or the program ends, so it is safe to return a pointer to it. p 523

9.8 What does throwing an exception mean?

Throwing an exception means the program signals that an error has occurred. p 519

9.7 A pointer can be used as...

a function parameter. It gives the function access to the original argument, much like a reference parameter does. p 509

9.7 Another way to pass an argument by reference is to use...

a pointer variable as the parameter. p 509

9.3 You've learned, in chapter 7, that an array name without brackets and a subscript,

actually represents the starting address of the array. This means that the array name is really a pointer. p 500

9.4 An integer may be

added to or subtracted from a pointer variable. This may be performed with the + and - operators, or the += and -= operators. p 505

9.8 Under older C++ compilers, the new operator returns the..

address 0, or NULL when it fails to allocate the requested amount of memory (NULL is a named constant, defined in the iostream file, that stands for the address 0). p 519

9.8 Programs can only access newly allocated memory through its...

address, so a pointer is required to use those bytes. p 518

9.7 When you are writing a function that uses a pointer parameter, and the function is not intended to change the data that the parameter points to...

it is always a good idea to make the parameter a pointer to const. Not only will this protect you from writing code in the function that accidentally changes the argument, but the function will be able to accept the addresses of both constant and non constant arguments. p 515

9.5 When a pointer is initialized with an address

it must be the address of an object the pointer can point to. p 506

9.1 When the address operator & is placed in from of a variable name...

it returns the address of that variable. &amount p 492

9.3 Remember that C++ performs no bounds checking with arrays. When stepping through an array with a pointer...

it's possible to give the pointer an address outside of the array p 502

9.2 Pointers are also very useful in algorithms that

manipulate arrays and work with certain types of strings. p 495

9.4 Some ________ ________ may be performed on pointers.

mathematical operations. p 504

9.4 The contents of pointer variables may be changed with

mathematical statements that perform addition or subtraction. p 504

9.2 A pointer variable holds a

memory address. p 493

9.7 When a function is called, the address of the variable that is to be doubled...

must be used as the argument, not the variable, itself. p 510

9.8 A C++ program requests dynamically allocated memory through the _____ operator.

new p 518

9.8 Only use pointers with delete that were previously used with

new. If you use a pointer with delete that does not reference dynamically allocated memory, unexpected problems may result. p 520

9.8 If you know the amount of variables needed in a program, you can define them...

up front. p 518

9.4 ++ and -- may be

used to increment or decrement a pointer variable. p 505

9.2 Pointer variables also allow you to

work with the data that they point to. p 493

9.3 In C++, when you add a value to a pointer,

you are actually adding that value times the size of the data type being referenced by the pointer. *(numbers + 1) is actually *(numbers + 1 * 2) p 500

9.2 When you are working with a dereferenced pointer

you are actually working with the value that the pointer is pointing to. p 497

9.2 When you apply the indirection operator (*) to a pointer variable

you are working, not with the pointer variable itself, but with the item it points to. p 498

9.2 In order to make a pointer variable reference another item in memory

you have to write code that fetches the memory address of that item and assigns the address to the pointer variable. p 495

9.9 Functions can return pointers but...

you must be sure the item the pointer references still exists p 522


Conjuntos de estudio relacionados

Chapter 39 - The Child With a Genitourinary Disorder

View Set

TEXAS POLITICS REVIEW 2 (chapter 4&5)

View Set

GRE Math Definitions, Formulas and Problems (Algebra)

View Set

MANGT 595 - Final - Quiz Questions

View Set