No more fear of Pointers

Variable Allusion

Puneet Sapra
Feb 4, 2018 · 5 min read

Pointers are the essential concept of programming. These are most feared and misunderstood. “A little knowledge is a dangerous thing” fits well with Pointers.

At first, pointers are hard to grasp. Everything you started in the beginning, is hard too.

C and C++ support exhaustive features of pointers. Other languages, adopted a subset features of pointers.

No matter which language you into, it is still worth to understand.

This article assumes that you are aware of the basics of C or C++.

🚉 First Stop — Variable

A variable is a memory container where value resides.

variable declaration syntax

A variable can be associated with

  • A name is what you call the variable; it is an essential element of communication among programmers.
  • A type defines the domain of values that can be stored inside variable, encoding scheme (for primitive type) and the memory size of the container.
  • An address is where a variable resides in the memory; it may be physical or logical.

In memory, each byte is addressed for the processor to access the value stored. A variable may span multiple bytes.

A variable of int type holds 4 bytes in memory (4 bytes span). We address a variable by its first-byte address.

The address of a variable always means the address of its first-byte.

🤔 Why we address a variable by its first-byte address?

The type defines the size of a variable. A multi-byte variable is always stored in contagious locations (established norm). If size and starting address are known, all the bytes of a variable are known.

🚉 Last Stop — Pointer

A Pointer is a variable, points to another variable. More precisely, the value of a Pointer is a memory address (starting address) of another variable.

& (address-of) operator is used to getting the address of a variable.

Pointers are easy to follow if these are visualized graphically.

Declaration examples

A memory address is a number, so the type of pointer is always Integer despite whom it is pointing.

If a pointer is an integer then what about the type we set at the time of declaration?

It is the type of referred variable and only required if pointer arithmetic is required else not.

Let say it declared pointer type for the sake of brevity. void * (Generic pointer) is used if the referred variable type is not known.

Why we need pointers?

A pointer facilitates indirect access to a variable. This feature enables the passing of variables to functions efficiently.

Typically, when we pass values to a function, these values are copied to function parameters. It can be understood as:

int first = a; // value of a is copied to first
int second = 2; // 2 is copied to second

The copying does not cause any problem for scalar (single-valued) types, but it would be a performance hit for multi-valued types (string, array, custom type).

So what is the solution? Send the values indirectly. Pointer facilitates the indirect passing of the values.

How do I access the referred variable’s value back through the pointer?

Value-at or Deference operator is used to access the variable to which a pointer is pointing. Sadly, symbol * (asterisk) has been reused for this purpose.

Let’s see it in action:

Run it →https://repl.it/repls/LikableIdolizedGoldfinc

In the case of string and array, the value can be access through [] index-of operator. The following expression is equivalent:

pointer[i] = *(pointer + i)  (see next section)

You can interchange an array with a pointer for accessing and updating values.

Basically, an Array is a Pointer to the first element of Array.

Run it → https://repl.it/repls/WideeyedImpartialArrowworm

Thinking Mathematically

The beauty of pointers is that you can visualise their assignment statement as algebraic equations.

By thinking this way, you can get cosy with pointers to any level 😉.

Pointer to Pointer example

Pointer Arithmetic

An integer can be added or subtracted to/from a pointer. This construct can be used to iterate an array or a string. But remember, you can do such math only if you have defined the declared type of the pointer.

Adding a number to a pointer follows the following equation:

new-location = old-location + number x sizeof(<type you declared>);

Visually, it can be imagined as

Run it → https://repl.it/repls/WideCoralVelvetworm

Dynamic allocation of Memory

Pointers also enable to create dynamically sized memory buffers or arrays or string.

😎 Show off your skills with Pointers to Function

Pointers can also point to Functions; which helps in passing Functions to Functions.

type (*function_pointer)(parameters)
Pointer to function

Some definitions are associated with pointers:

  • Dangling Pointer points to a location which is no more valid and accessing such location cause segmentation fault.
  • Null Pointer does not point to any memory location/variable.

Tip to reduce headaches

Wherever possible, imagine pointer graphically and deals with them mathematically to avoid unnecessary confusion and headaches.


Pointers Beyond C and C++

  • In languages like Java, JavaScript, References are pointers equivalent. References can be considered the mini version of pointers, i.e. simplified syntax, no arithmetic.
  • In language GO, pointers have similar syntax and allowed operations (except pointer arithmetic).

The more you clap, the more this article find its audience, or you want to keep it to yourself.

The Mighty Programmer

Software Design and Architecture Series | More at…

Puneet Sapra

Written by

Software Design Enthusiast. https://themightyprogrammer.dev/ https://www.linkedin.com/in/saprapuneet/

The Mighty Programmer

Software Design and Architecture Series | More at https://themightyprogrammer.dev

More From Medium

More on Coding from The Mighty Programmer

More on Coding from The Mighty Programmer

Uploading files over the web

More on Programming from The Mighty Programmer

More on Programming from The Mighty Programmer

Custom Event with RxJS

More on Programming from The Mighty Programmer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade