DEV Community

Ebbe Wertz
Ebbe Wertz

Posted on

The unspoken question: "Why do pointers exist?"

It's Really A Valid Question

This is not the typical pointer-hater's question, but rather a very interesting one.


Here's What I Mean

Pointers are a great powerfull concept, but that's what it is: a concept. So why was the C compiler invented with such an isolated piece of logic and syntax dedicated to pointers?

Don't get me wrong, I love the oppurtunities that pointers give us and their current syntax. They are an absolutely essential feature, having achieved the evolution of dynamic data structures, objects and classes, multithreaded memory sharing, object mutability, low-redundant value duplication and much more.
But if you imagine the event of the invention of C, the pointers as current-day seem a much more impressive idea then an intuitive first concept. Let's take a deeper look into what I mean.


A Deeper Look

If you look at a pointer's structure it is basically an unsigned long (aka 4[32bit system] or 8 bytes in memory). The things that separate pointers from unsigned longs are the pointer-specific features.

Syntax And Dereferencing Operator

A pointer has its own declaration syntax and has its propertary operator: the dereferencer.

int a = 5;
int *ptr = &a; //declaration
int value = *ptr; //dereference
Enter fullscreen mode Exit fullscreen mode

But let's imagine, that this was never invented. Then the following would just be easily possible if the dereferencing feature was just associated with any integer type:

int a = 5;
unsigned long adress = &a;
int value = *adress;
Enter fullscreen mode Exit fullscreen mode

In that case, you could even do things like this:

int firstIntInMemory = *(0); //manually dereferences (4bytes at) adress 0`
Enter fullscreen mode Exit fullscreen mode

Speaking of the parser, this is totally not a conflicting syntax since star as derefrencer is a unary operator while star as arithmetic multiplicator is always a binary operator.
This fictional dereferencing operator as I described above is really the raw essence the pointer concept. Comparing this to the current real implementation makes the head question so interesting to think about. There could have been so many outcomes.

Pointer Arithmetic

The only special thing pointer arithmetic does is taking type sizes into accound with calculations. When I have an array, and I want to get the second element, I just add 1 to the pointer. If it is an int pointer, this wil implicitely actually add a value of 4 to the adress (if sizeof(int) == 4 on your system):

int arr[5] = {1,2,3,4,5};
int second = *(arr + 1);
Enter fullscreen mode Exit fullscreen mode

But let's be honest, the following is actually much more logical if you think intuitively about memory:

int arr[5] = {1,2,3,4,5};
int second = *(arr + sizeof(int));
Enter fullscreen mode Exit fullscreen mode

And this would just be standard integer arithmetic. If you look at it this way, there is not really a reason to have invented pointer arithmetic at all.


That's Not All

Off course, the '*'-syntax makes intended usages much more clear. If you see it, you immeditately know that this variable is used for memory manipulation. Also every memory manupulation library function is designed for pointers.

But still, if it was never invented, and instead we had these dereferencable unsigned longs, people would have just come up with design- and naming conventions, like appending pointer variable identifiers with a '_p' suffix. And memory manipulation libraries would just have evolved around this.


Final Word

So really, if you think about it, C could just have survived the same way as it lives right now if pointers were never invented as a feature of the language. They would just be invented as a concept by programmers, working the same as how they currently exist.

I find this an interesting story to investigate deeper into.
Why did C invent pointer?
Was it just the reason we expect: consistency, clarity and safety against misuse of derefrencing?
Or is there a deeper reason and a much more complex logic then how I covered pointers in this post, which makes them actually significantly more efficient than doing the same with general purpose integers?

Top comments (2)

Collapse
 
pauljlucas profile image
Paul J. Lucas

You're conflating pointers with C. C has pointers, but Dennis Ritchie (the creator of C), did not invent pointers. The actual history of pointers pre-dates C by several years. C just has a peculiar declaration syntax for pointers. Other languages that have pointers have their own syntax, e.g., Pascal (that predates C by 2 years).

C’s predecessor B was a typeless language in that everything was a “word” so it effectively is your “what if never invented” example — except it was reality.

Perhaps you might do a bit more research before writing an article.

Collapse
 
gberthiaume profile image
G. Berthiaume

Pointer in C program is like butter in French cuisine: An essantial to create something good.