This blog post will cover the basics of pointers, a programming tool that is used in languages like C and C++. In this post, we will be using C as our primary language. Pointers are variables that contain a memory address (a concept used to access the computer’s primary storage memory). Variables normally contain a value such as 1 or ‘a’, but pointers contain an address of the value. When we reference a variable through pointers, this is called indirection. Each link goes to a text file of C code. This code can be run as is and will help show us the power of pointers!
The “&” (ampersand) operator is the address operator, using the ampersand will return the address of a variable.
The “*” (asterisk) operator is a pointer to a variable.
Using pointers allows us to pass variables into functions, and then manipulate the values of these variables inside the function, and have them persist after the function is out of scope. Without pointers, this is impossible!
With pointers, we can change the entire array in bubblesort, and these changes will remain persistent even after the bubblesort function is no longer in scope!