Chapter 12 - Pointers and Dynamic Memory Allocation

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

To get the address of a data variable, (Arrays store address in name, so they are exempt from needing to use this operator), one must use the ________________________.

&, or "Address of" Operator

What is the proper way to use subscript and the indirection operator to offset an array?

*(<source data type><pointer name>[offset in bytes])

Write a program using two functions that change data permanently using pointers. Compare function to the other side of card.

//Use two functions that change data permanently using pointers. #include <iostream> using std::cout; int function1(int * number); int function2(int * number); int main() { int data = 2; int data_address = function1( & data); cout << data_address << "\n"; return 0; } //Simply passes the int data address along to function2 int function1(int * number) { int * address = number; int data_original_location = function2(address); return data_original_location; } int function2(int * number) { int multiplier = *number * 5; return multiplier; //Add 5 to the original address }

Describe in lay terms what a pointer with multiple levels of indirection is.

A pointer with more than one star, *, **, ***, ****, etc., is a pointer to the address of the identifier of a pointer, the address of a location where another address is being stored; recursive levels of indirect addressing to an original source of data, (The data variable)

What is a pointer?

A variable that holds an address; the address can change.

What is the syntax for accessing 2-dimensional array with a pointer variable?

You cannot pass the name to pointer variable, 2-dim array is array of arrays. <Array-Type> * <pointer-identifier> = <Array-Identifier> [row-number] This works because a single row of 2-dim array is storing a single address in the identifier which is an integer specifying the row

What is correct in C, but incorrect in C++ for initializing the pointer to a null value?

char * ptr = NULL;

What is correct syntax for declaring multiple pointers in a single declaration?

char * str1 = nullptr, * str2;

Write a while-loop to iterate through an array and compare the data at each address using pointer arithmetic.

char string[ 10 ] = { 0 }; char * current = string; while (*current != '\0') current++;

What would CIN look like within a function if you needed to permanently change a variable in main with CIN?

cin >> * <identifier>;

The target type of a pointer must ____________________ of the value stored at the address being pointed or referenced to.

match the data type

What reserved word is needed to initialize a pointer in c++?

nullptr

What is the syntax to declare a pointer?

<target-type> * <variable name>; Target Type: Any data type *(Asterisk): Indicates the containment of an address.

What is the syntax for a pointer with multiple levels of indirection?

<type> <identifier> = <data>; <target type> * <identifier> = & <target identifier>; <target type> ** <identifier> = & <target identifier>;

Do you need to pass by reference again when calling a function inside of another function to pass the original value from main?

No, the address will not change unless desired, and the original data will remain accessable with only one star, or one level of indirection

How do you permanently change the data at an address from a variable declared in main, from inside a function?

Pass the variable by reference, int mine = 99; . . . int function (int & number){ //Now this can change the data at the address specified. }

Define (Textbook): Indirection

The method of referencing variable in the roundabout way, rather than directly accessing the variable by its name. Accessing a value via its memory address rather than the variable name is an example of indirection

When a pointer is incremented how are types handled?

This matters in arrays. does it go by a single memory address, (64-bit, so 8 bytes), or does it gather the memory allocated for a variable type and increment by the spaces as wide as the allocated memory depending on the type?

What is the syntax for a for-loop outputting an array using pointer arithmetic?

for(i = 0; i < \array-length\; i++, ptr++) { //dereference, then print out data at address cout << *ptr << endl; }


Ensembles d'études connexes

Chapter 1 Review: Introduction to Animals

View Set

Chemical Change Definition in Chemistry (questions on article)

View Set

Neuroanatomy: cortical anatomy, thalamus, basal ganglia, hypothalamus, hippocampus

View Set

Ch.4 Life Insurance Premiums, Proceeds and Beneficiaries | NOTES

View Set

цифры(деньги,математика,даты,тел номер, счёт больше 60)

View Set

Primavera U.S Government A WORKBOOKS L1-L30 (2019)

View Set