Computer Science Fundamentals: Pointers

Here I revisit my UC Davis roots and attempt to track down Sean Davis

Curt Corginia
5 min readAug 19, 2022
Photo by Víctor Martín on Unsplash. Get it?

Here is the tl;dr:

  • A pointer is defined as an object that stores a memory address
  • The point of pointers is efficiency. It is easier to give someone the address of your house than it is to carry your house with you (I don’t like the analogy, but I can’t think of a better one; I believe VSauce likened it to a table of contents)
  • In C and C++, pointers can be used to return multiple parameters in a single function; they have some other uses as well
  • The reason I am revisiting this is that I am now convinced a huge portion of modern coding interviews are built around pointers

To elaborate on the last point, this is a 2006 post someone sent me by one of the co-founders of StackOverflow. I think the person who wrote it had a tremendous impact on coding interviews as we know them today, and he had this to say:

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.

One wrinkle in this theory of mine is that, in all likelihood, the vast majority of interview candidates today do not use C or C++ in coding interviews. By this logic, pointer questions live on in the form of tree and linked list questions.

Talk About Memory Lane

Mini-anecdote: On my first month as a software engineer, another recent college graduate tried to explain pointers to me by giving me a google link to this page. I was pretty annoyed…at my college, pointers were something of a rite of passage. And yet, even then, I found myself coming back to that page to clarify some things.

The question I had asked was whether pointers can be both inputs and outputs in a function. The second argument we had was about C++ references. I will not elaborate much on either conversation today, but the answer to the first question is yes, and a response to the second argument is that C++ references are generally easier to work with than pointers.

A Breakdown

I like the background music.

The one issue I have with the YouTube video above is its assertion that “you will never ask about pointers again after watching it.” What about pointer arithmetic? What about void pointers?

If you put a star next to a variable, it means you are declaring a pointer to a variable. This is not to be confused with the deference operator, which is used to access the value the pointer pointers to. In other words, the * has two uses — it can be used in a pointer declaration, and it can be used to dereference.

Me: Is this good code? Please tell me if this is not good code, because I am going to share it on my blog. If I share it on my blog and it is not good code, someone in the comments is going to tell me to go kill myself
Smack: I mean, the “hello world” part is solid

The first num is 100, the next num is 88.

The ampersand sign is the “address-of operator.” It can be thought of as the complement to *.

In college, there was one example they threw at us until we were tired of it. Though I do not remember exactly how it went, they were basically trying to demonstrate how pointers can be used to get around scoping issues (if you watch the YouTube video above, it also demonstrates this concept). Global variables are generally looked down upon, but I think one problem with how we were taught is we came away with the idea that the main point of pointers is to avoid global variables — anyone who used a global variable on a homework assignment instantly failed.

pointerlessFoo is a standard function you are probably familiar with even if you do not know anything about pointers. It accepts 88 as a parameter. It attempts to set that value to 100. But…what’s this?! All it has managed to do is create a local variable that goes out of scope and is lost. It has not done anything to get the value of 100 back into main.

Compare this to lines 15 and 16. Line 16 uses & in its call — again, this is the complement to *. Foo, in lines 8–11, actually uses the dereference operator to modify the value of originalNum itself. Because of this, originalNum actually becomes 100.

You can read more about it here — it is helpful to see actual diagrams of memory addresses. The video is fairly succinct as well.

Why Should I Care?

  • An array works like a pointer to the first array element.
  • You can also use character pointers.
  • It is necessary to understand pointers when doing linked list problems in coding interviews
  • Efficiency, getting around scoping issues, and working with the stack and heap…which the video covers

Closing Thoughts And Searching For Sean Davis

Source: https://www.reddit.com/r/UCDavis/comments/g4yk4x/what_is_sean_davis_up_to/
Source: https://www.ratemyprofessors.com/ShowRatings.jsp?tid=35125

Sean Davis was consistently rated as the most dedicated professor in our department. He retired in 2017, apparently re-emerged for a single quarter, then left again.

Remembering the college experiences was oddly comforting, but I did not enjoy any of it at the time. This was, in my experience, a pretty challenging program. I was unable to find any of my ECS30 (the very first class, which was in C) assignments, but I did see that a new version of the assignments is just kind of…up.

For the world to see.

There are even some midterms there. So anyone who reads this blog can take all the ECS30 midterms their heart desires and maybe self-study 10 weeks worth of computer science at a very middle-tier American college.

I emailed Sean Davis, but he didn’t respond. It was a link to the recreation of the first web browser ever. I thought he would care a lot about that, for some reason.

--

--

Curt Corginia

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