What are pointers?
If you are like me and you started programming with a higher level language, the first time you encounter pointers in the blogging realms it gives you the impression that you have arrived at the gates of Hell. The reality is that the concept is not that difficult and using them gives you certain advantages. However care must be taken.
So what are pointers? the down and dirty version!
Pointers are just a data type that contains a memory address. There you go. Plain and simple. If you print a pointer, it will print out an address in hexadecimal format — 0x0bff03764.
The purpose of pointers is to access memory chucks directly. They reduce execution time, storage space, and complexity, while allowing to create complex data types. When a program needs to pass data to a function it can do so by either copying the data and giving the function a copy of that data, or it can pass a reference to where the original data is so that the function can work directly that data. This, by the way, the the famous known passing-by-reference versus passing-by-value. When using pointers, you are passing-by-reference, so the program is not creating new copies of the data, which in turn require more memory to be allocated, and instead its passing a reference address of the original data.
How to declare a pointer in C:
int justANummber = 21;
int *myPointer;
myPointer = &justANumber;
There it is. You have a pointer in C. So what creates the pointer? The pointer operator “*” does. It just happen to be the same operator as multiplication. However, the placement and context within your program will determine its function. In this case, when declaring a variable, it is a pointer. Another important concept is that all pointers must point to a pointee. The pointer is the pointer variable; the pointee is the variable that the pointer points to, using the pointees memory address.
On this post I won’t get into memory mamagement, however it is a very important topic that goes hand-in-hand with pointers and I will conver it on a future post. The only portion that I will cover is that when using a pointer, you must always free the memory being used for it. If you don’t, you will have a memory leakage.
You free the pointer by using the keyword “Free()”. This is how we would free the previously created pointer:
Free(myPointer);
Pointers in Swift
Get The CodeNeophyte’s stories in your inbox
Join Medium for free to get updates from this writer.
As a Swift developer, I find it interesting that it also allows you to declare pointers. This, however, is called unsafe Swift. Normally, when developing Swift Apps, you dont need to use pointers and Swift runs “memory safe”. But when you need to use C libraries or interact with a C API, among other uses, then you may need to access pointers in Swift. Althought Swift has many pointer types, we are just going to look at 2: UnsafePointer<T> and UnsafeMutablePointer<T>.
UnsafePointer is used for accessing data of a specific type. It is read-only.
UnsafeMultablePointer is used for accessing and manipulating data of a specific type. It allos read and write.
How to declare a pointer in Swift:
var toys = 100
var otherToys = 50
let myToysPointer = UnsafePointer(&toys)
let myOtherToysMutablePointer = UnsafeMutablePointer(&otherToys)
myOtherToysMutablePointer.pointee = 2000 //Just changed the value of otherToys to 2000 from 50
Thats it. Now you have pointers in Swift and can manipulate data with them. Keep in mind that this is just a basic overview and that this is an advanced subject. I will cover Swift pointer types in more detail on another post.
If you want to learn more about pointers and computer science, use these resources:
Gribble Lab’s C Programming BootCamp: http://www.gribblelab.org/CBootCamp/index.html
CS Resources (A constantly updating collection of quality CS resources): https://github.com/CodeNeophyte/CS_Resources
Interacting with C APIs: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html
Unsafe Swift: https://www.raywenderlich.com/148569/unsafe-swift









