0
0
C++programming~5 mins

Pointer and variable relationship in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a pointer in C++?
A pointer is a variable that stores the memory address of another variable.
Click to reveal answer
beginner
How do you declare a pointer to an integer variable?
You declare it using the syntax: int* ptr; where ptr is the pointer variable.
Click to reveal answer
beginner
What does the * operator do when used with a pointer?
It dereferences the pointer, meaning it accesses the value stored at the memory address the pointer holds.
Click to reveal answer
intermediate
Explain the relationship between a pointer and the variable it points to.
A pointer holds the address of a variable. Changing the value through the pointer changes the original variable's value because they refer to the same memory location.
Click to reveal answer
intermediate
What happens if you change the pointer's value itself?
Changing the pointer's value changes the memory address it points to, so it will point to a different variable or memory location.
Click to reveal answer
What does the following code print?<br>
int x = 10;<br>int* p = &x;<br>std::cout << *p;
AGarbage value
BAddress of x
C10
DCompilation error
Which operator is used to get the address of a variable?
A&
B*
C->
D%
If int* p; is uninitialized, what happens if you try to dereference it?
AIt prints 0
BIt causes undefined behavior (likely crash)
CIt prints the address stored in p
DIt compiles but does nothing
What does the pointer store?
AA memory address
BA value
CA variable name
DA data type
How do you change the value of a variable using a pointer?
AYou cannot change variable value through pointer
BChange the pointer variable itself
CUse the & operator on the pointer
DUse the * operator to assign a new value
Explain in your own words how a pointer relates to the variable it points to.
Think about how a pointer is like a remote control to a TV.
You got /3 concepts.
    Describe what happens when you change the pointer's value versus when you change the value it points to.
    Consider the pointer as an address label and the variable as the house at that address.
    You got /3 concepts.