pointers and references practice
This page contains set of challenges that you can go through to test your understanding on the
topic. It aditionally contains links to resources and or explanations on the topic.
This page was last updated on 10.11.2019
You can find its source on github.
-
Pointer is a variable that can contain a memory address.
Recommended reading: The C Programming Language", 2nd edition, Kernighan and Ritchie; Chapter 5
Useful links: cppreference.com,
cplusplus.com,
Stanford 106,
random
pointer tutorial,
and more
pointers.
[+] Show
example code
string* ptrToElement(vector* const pVec, int i){
//returns address of the string in position
// i of vector that pVec points to
return &((*pVec)[i]);
}
// declaring some varibles
int zz = 1;
int zz2 = 343;
string szz;
//declare a pointer
int* pAPointer;
//declare and initialize a pointer
string * pTestpointer = nullptr;
// assign address of variable/object to the pointer
pAPointer = &zz;
pTestpointer = &szz;
// reassign pointer to new pointee
pAPointer = &zz2;
// modify object pointer points too (pointee) by deferencing
*pTestpointer = "test string\n";
// print pointer address then object it references
cout << pAPointer << " | " << *pAPointer << endl;
cout << pTestpointer << " | " << *pTestpointer;
/* constant pointers can only point to objects
they were initialized to point to. like all constants
they have to be initialized on declaration */
int* const pConstatnPointer = &zz;
// pointers to a constant cant be used to change
// value they point to.
const int* pZz;
/* constant pointer to a constant combines the
restrictions of a constant pointer and a pointer
to a constant. This means that a constant pointer
to a constant can only point to the object that it
was initialized to point to. In addition, it can’t
be used to change the value of the object to which
it points*/
const int* const pCPC = &zz2;
// when passing pointers to function
// we have to use address of object
pointerExample(&zz);
// ...which is same as using pointer
pointerExample(pCPC);
// returning pointers
vector tempVec(10,"spam");
cout << *( ptrToElement(&tempVec, 3) ) << endl;
// Assigning a Returned Pointer to a Pointer
string* pMagic = ptrToElement(&tempVec, 3);
*pMagic = "magic";
cout << tempVec[3] << endl;
// an array name is a constant pointer to
// the first element of the array
string tempArray[3] = {"foo", "bar", "spam"};
*tempArray="hehexd";
cout << tempArray[0] << endl;
A reference is a simple reference datatype that is less powerful but safer than the
pointer type inherited from C.
Useful links: cppreference.com,
isocpp.org.
[+] Show
example code
string& ref2element(vector& barVec, int i){
return barVec[i]; // returns reference
}
void swapNums(int& a, int& b){
int temp = a;
a = b;
b = temp;
}
int intD = 10;
// if we want to prevent reference modifying we use const
const int& rIntD = intD;
cout << rIntD << endl;
int intE = 2; int intF = 3;
swapNums(intE, intF);
cout << intE << " " << intF << endl; // 3 2
// small example of reference to reference
vector barVec;
barVec.push_back("foo");
barVec.push_back("bar");
// assignees returned reference to reference
string& rStr = ref2element(barVec,1);
cout << rStr << endl; // bar
rStr = "spaz"; // rStr == barVec[1]
cout << barVec[1] << endl; // spaz
-
1. Create pointer 'pNum' that points to int 'num'. Set value of num to 10 by deferencing pNum
pointer. Initialize new varibale 'num2' by directly acessing 'num' throught pNum pointer. Output
num2.
[+] Show
solution
int num;
int* pNum = #
*pNum = 10;
int num2 = *pNum;
std::cout << num2 << std::endl;
-------------------
output: 10
2. How would you output memory address of num from previous example? How would you output memory
address of num by using pointer? How would you ouput memory address of the pointer itself?
[+] Show
solution
std::cout << &num << std::endl;
std::cout << pNum << std::endl;
std::cout << &pNum << std::endl;
---------------
output:
0x28ff28 - num
0x28ff28 - num over pointer
0x28ff24 - pointers address
3. What does following code output?
void coutA(int*a){
cout << &a << " | " << a << " | " << *a << " | in pointer func" << endl;
}
void coutA(int&a){
cout << &a << " | " << a << "\t\t | in reference func" << endl;
}
int main() {
int a = 42;
int *pA = &a;
int **pA2 = &pA;
coutA(a);
coutA(&a);
coutA(pA);
coutA(*pA);
coutA(*pA2);
coutA(**pA2);
cout << endl;
cout << &a << endl;
cout << &pA << endl;
cout << &pA2 << endl;
[+] Show
solution
0x28ff28 | 42 | in reference func
0x28ff10 | 0x28ff28 | 42 | in pointer func
0x28ff10 | 0x28ff28 | 42 | in pointer func
0x28ff28 | 42 | in reference func
0x28ff10 | 0x28ff28 | 42 | in pointer func
0x28ff28 | 42 | in reference func
42
0x28ff28
0x28ff28
0x28ff24
0x28ff20
3. Initialize int and float variables 'foo' and 'bar' both to 0. Declare a new void pointer and
use it to change values of foo and bar to 3 and 3.4 respectively. Output values of foo and bar.
[+] Show
solution
int foo = 0;
float bar = 0;
void *pointy;
pointy = &foo;
*(int *)pointy = 3;
pointy = &bar;
*(float *)pointy = 3.4;
std::cout << foo << ", " << bar << std::endl;
--------------------------------------------
output: 3, 3.4
4. Define an array of first 5 natural numbers. Output whole array by deferencing a pointer and
change 3 to 4 before outputing it.
[+] Show
solution
int arr[] = {1,2,3,4,5};
for(int i = 0; i<sizeof(arr)/sizeof(*arr); ++i){
if(*(arr+i) == 3) std::cout << 4 << " ";
else std::cout << *(arr+i) << " ";
}
------------------------------------------------
output: 1 2 4 4 5
5. What does following code output?
int a = 1;
int b = 2;
int c = 3;
int arr[] = {a,b,c};
*(arr+2) = 7;
for(int i = 0; i<sizeof(arr)/sizeof(*arr); ++i){
std::cout << &arr[i] << " | " << arr[i] << std::endl;
std::cout << arr + i << " | " << *(arr+i) << std::endl;
}
[+] Show
solution
0x28ff14 | 1
0x28ff14 | 1
0x28ff18 | 2
0x28ff18 | 2
0x28ff1c | 7
0x28ff1c | 7
6. Define new pancake using struct bellow and assign it values 10 and 7.68 using pointers.
Output pancake info using alternative notation.
struct Pancake {
int i;
float f;
};
[+] Show
solution
Pancake *ptr, pancake;
ptr = &pancake;
(*ptr).i = 10;
(*ptr).f = 7.68;
std::cout << ptr->i << " " << ptr->f;
------------------------------------
output: 10, 7.68
No © Use as you please