Welcome 🤗! In the next ~4h we will cover:

Footpath signs, Clem Rutter, CC BY-SA 3.0, Source
Motivation¶
Every value in computer memory lives at an address, like a house on the street. Often you don’t want the value itself. You want to know where it is, so you can find it later, share it with another part of the program, or change it in place.
- pointer
a variable whose value is a location in the computer’s memory. Pointer (computer programming)
Learning goals¶
Explain the motivation for pointers
Define and use pointers including
*and&operatorsPass arguments to functions by reference, write functions that accept pointers
Differentiate between constant pointers and constant values based on the
constsyntaxUse
sizeofoperatorExplain the role of pointers in arrays and strings
Preparation Review¶
Code without main¶
In the following you will see code snippets like this:
#include <stdio.h>
int i = 42;
i(int) 42(int) 42 means that the variable i is of the type int and has the value 42. These code cells are simplified programs without main and printf for teaching purposes [1]
E: Teach your peers¶
Question pool¶
Coding¶
Hello pointer¶
Pointærray¶
Complete the following steps based on the following template:
#include <stdio.h>
constexpr size_t SIZE = 11;
int main() {
int nums[SIZE] = {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5};
// YOUR CODE HERE
}
Template
Define a pointer
ptrthat can point to the elements ofnumsMake the
ptrpoint to the first element ofnums.Print the elements of
numsusingptr.Make the
ptrpoint to the middle element ofnums.Print the elements of
numsafter and including the values thatptrpoints to. Useptrfor this.
-5 -4 -3 -2 -1 0 1 2 3 4 5
0 1 2 3 4 5
Expected output
Arrays as arguments¶
Write and demonstrate two functions. Use const where applicable, especially in function arguments.
sumthat takes an array and the array’s size. It returns the sum of the elements of the array, e.g., the sum of{1, 2}is 3.vector_sumthat returns the sum of two arrays, e.g.,{1, 2}+{2, 3}={3, 5}. How you design the return value or arguments are up to you 🙃.
E: Reflection¶
Let us revisit our learning goals:
Explain the motivation for pointers
Define and use pointers including
*and&operatorsPass arguments to functions by reference, write functions that accept pointers
Differentiate between constant pointers and constant values based on the
constsyntaxUse
sizeofoperatorExplain the role of pointers in arrays and strings
These interactions are based on the
clang-replprogram, which is like a shell for the C++ language or REPL.clang-replprogram does not seem to be part of the compiler package we installed on Windows. It may be available in theLLVM.LLVMpackage, which I did not test.To generate the results on this website I use a plugin that runs
clang-replin the background: Jupyter notebook running a Clang-Repl-based kernel. I use them there to explain how code works.The book has 200 pages, each page about 200 words, and each word has about 5 letters. If we also account for the spaces between letters, the content would correspond to about .