Chap 9

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

The____operator can be used to determing a variable's address.

&

Look at the following array definition. int set[10]; Write a statement using pointer notation that stores the value 99 in set[7];

*(set+7)=99;

Look at the following code. int x = 7; int *iptr = &x; What will be displayed if you send the expression *iptr to cout ? What happens if you send the expression ptr to cout ?

*iptr will display the value, but iptr will display the address

What math operations are allowed on pointers?

++, --, an integer may be added or subtracted from a pointer, a pointer may be subtracted from another.

Under older compliers, if the new operator cannot allocate the amount of memory requested, it returns

0

So far you have learned three different uses for the * operator. What are they?

1. Multiplication 2. Definition of a pointer variable 3. indirection operator

What are two advantages of declaring a pointer parameter as a constant pointer?

1. The parameter will be initialized with the address that is passed as an argument into it. 2. Cannot point to anything else while the function is running. can accept the address of both non-constant and constant arguments.

Under what circumstances can you successfully return a pointer from a function?

1. a pointer to an item that was passed into the function as an argument, 2. a pointer to a dynamically allocated chunk of memory.

Look at the following array definition. int numbers[] = { 2, 4, 6, 8, 10 }; What will the following statement display? cout << *(numbers + 3) << endl;

8

What is the difference between a pointer to a constant and a constant pointer?

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

TF A pointer variable that has not been initialized is called a null pointer.

F

TF Any mathematical operation, including multiplication and division, may be performed on a pointer.

F

TF Array names cannot be dereferenced with the indirection operator.

F

TF In using a pointer with the delete operator, it is not necessary for the pointer to have been previously used with the new operator.

F

TF The & operator dereferences a pointer

F

TF The & symbol is called the indirection operator

F

TF The * operator is used to get the address of a variable.

F

TF You can change the address that an array name points to.

F

What does the indirection operator do?

It deferences the pointer meaning that you are dealing with the value that it is pointing to.

_____ variables are designed to hold addresses.

Pointer

TF Each byte of memory is assigned a unique address.

T

TF Pointer variables are designed to hold addresses

T

TF The address operator is not needed to assign an array's address to a pointer.

T

TF The new operator dynamically allocates memory

T

TF When the indirection operator is used with a pointer variable, you are actually working with the value the pointer is pointing to.

T

TF When used as function parameters, reference variables are much easier to work with than pointers

T

TF When you add a value to a pointer, you are actually adding that number times the size of the data type referenced by the pointer.

T

TF pointers may be compared using the relational operators.

T

TF the address 0 is generally consider unusable

T

Each byte in memory is assigned a unique _____

address

Write the definition of ptr , a pointer to a constant int.

const int * ptr;

Write code that dynamically allocates an array of 20 integers, then uses a loop to allow the user to enter values for each element of the array.

const int SIZE=20; int *iptr=new int[SIZE]; for(count=0; count<SIZE; count++) { cout<<"Enter element"<<count+1<<endl; cin>>int[count]; }

Array names can be used as______, and vice versa.

constant pointers

Look at the following code. double value = 29.7; double *ptr = &value; Write a cout statement that uses the ptr variable to display the contents of the value variable.

cout<<*iptr<<endl;

When a program is finished with a chunk of dynamically allocated memory, it should free it with the __________ operator.

delete

Assume that tempNumbers is a pointer that points to a dynamically allocated array Write code that releases the memory used by the array.

delete [] tempNumbers;

Creating variables while a program is running is called __________.

dynamic memory allocation

What is the purpose of the new operator?

dynamic memory allocation in order to set aside a chunk of memory for a specific data type.

What is the purpose of the delete operator?

free memory that was created by the new operator.

The_____operator can be used to work with the variable a pointer points to.

indirection.

Write the definition of ptr , a constant pointer to an int.

int *const ptr;

Assuming that ptr is a pointer to an int , what happens when you add 4 to ptr ?

it multiplies four by the number of bytes and adds it to the memory address.

What happens when a program uses the new operator to allocate a block of memory, but the amount of requested memory isn't available? How do programs written with older compilers handle this?

it throws an exception and terminates the program.

The____operator is used to dynamically allocate memory.

new

You should only use pointers with delete that were previously used with _____.

new

A pointer that contains the address 0 is called a(n) __________ pointer.

null

Look at the following function definition. void getNumber(int &n) { cout << "Enter a number: "; cin >> n; } In this function, the parameter n is a reference variable. Rewrite the function so that n is a pointer.

void getNumbers( int *n) { cout<<"Enter a number: "; cin>>*n; }


Set pelajaran terkait

Domain and Range of Trigonometric Functions

View Set

Крок М педіатрія(2 семестр)

View Set

environmental ethics first half of semester

View Set

Chapter 9: Federal Tax Considerations for Accident and Health Insurance

View Set

CSCI 211 Chapter 1-6, CSCI 211 Chapter 7-10 Questions, CSCI 211 Chapter 11-14 Questions

View Set

Giddens Ch 38 Interpersonal Violence EAQ

View Set

Chapter 27, Hygiene and Personal Care & Skin integrity/Wound Care

View Set

6-2 Earthquakes and Seismic Waves

View Set