Computer Science Fundamentals — Pointers, Revisited

Because Life Is Not Complete Without The Asterisk

Curt Corginia
4 min readSep 15, 2023
Photo by Víctor Martín on Unsplash. I am reusing this photo from last year.

About a year ago, I started (and apparently never continued) a series called “Computer Science Fundamentals.” You are welcome to read it, but it is a bit of a mess — it gets really bogged down in personal anecdotes about my college experiences, as well as this quote by Joel Spolsky, the creator of Trello and co-founder of StackOverflow.

I’ve come to realize that understanding pointers in C is not a skill, it’s an aptitude. In first year computer science classes, there are always about 200 kids at the beginning of the semester, all of whom wrote complex adventure games in BASIC for their PCs when they were 4 years old. They are having a good ol’ time learning C or Pascal in college, until one day the professor introduces pointers, and suddenly, they don’t get it. They just don’t understand anything any more. 90% of the class goes off and becomes Political Science majors, then they tell their friends that there weren’t enough good looking members of the appropriate sex in their CompSci classes, that’s why they switched. For some reason most people seem to be born without the part of the brain that understands pointers. Pointers require a complex form of doubly-indirected thinking that some people just can’t do, and it’s pretty crucial to good programming. A lot of the “script jocks” who started programming by copying JavaScript snippets into their web pages and went on to learn Perl never learned about pointers, and they can never quite produce code of the quality you need.
Source

  • A pointer is an object that stores a memory address
  • A pointer allows for more efficiency
  • In C and C++, pointers can be used to return multiple parameters in a single function, and to avoid the generally frowned-upon usage of global variables
“No lunch for you.” Image source: https://www.teamblind.com/post/Ok-to-use-global-variables-in-interview-ixRSzqLY

Here is the original blog post I wrote a year ago

First Stop: Low Level Learning

Say we have the line

int x = 4;

This line of C, Low Level Learning explains, allocates memory on the stack so that the value of 4 is now stored at a particular address. For the sake of example, he draws a chart and states that the address for value 4 is 0x1000. That is a hex value.

Now, he continues, consider this line

int* px = &x;

In English, this can be read as: The integer pointer named px is set to the value of address x. A pointer is just a value that happens to be an address. & is the address-of operator. A pointer is a variable that stores the memory address of an object. The * and & symbols can be thought of as complements.

Low Level Learning concludes by stating that pointers allow programmers to avoid global variables, and (as a separate use) to reduce space by not making copies.

The line int* px = &x declares a pointer to a variable. * has a second use, which is to dereference. The dereference operator is used to directly access a variable a pointer points to. This was confusing as hell for me until I had to use it in actual code. Even in the field, I constantly found myself revisiting this webpage.

In case these two lines of code do not explain very much out of context, here is the example I made a year ago. It was me doing my best to recreate a college example we saw over and over again.

Print results: First num: 100, Next num: 88

Foo is a method that uses pointers. It accepts the address of originalNum, value 42, as a parameter. Foo uses the derefrence operator to modify the value of originalNum itself. The value of originalNum changes from 42 to 100.

pointerlessFoo, in contrast, has no impact on originalNum2. It accepts 88 as a parameter, attempts to set the value to 100, but actually just creates a local variable that gets set, goes out of scope, and is then lost. The value of 100 from pointerlessFoo never makes it back to main, whereas the one passed to foo does.

Applications

An array works like a pointer to the first array element.

You can use character pointers, and the topic of pointers is fundamentally connected to how linked lists work, regardless of whether or not you are a C/C++ programmer.

Resources And Closing Thoughts

This is my class’ syllabus, a year after I took it

That syllabus page links to this 30-page explanation of pointers by a Stanford professor.

That Stanford webpage also links to a video called “Binky Pointer Fun Video.” This YouTube link is not official, but I do not think they would mind the fact that it exists, considering they wanted this Stanford resource to be open to the public (I could be wrong, though; I am not the one who uploaded this to YouTube)

I’d like to continue the series and revisit my old college notes, especially since some things like the virtual table, polymorphism, and dynamic_cast have come up for me in actual interviews. But first I wanted to redo the “pointers” blog post because I thought it was a mess.

--

--

Curt Corginia
Curt Corginia

Written by Curt Corginia

Founder, CEO, CTO, COO, and janitor at a company I made up called CORGICorporation

No responses yet