No more fear of Pointers

Variable Allusion | Pointers’ Primer

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

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

This article assumes that you are aware of basic programming in C or C++. It is a long read.

🚉 First Stop — Variable

A variable is a memory container where value resides.

variable declaration syntax
  • A type defines the domain of values that can be stored inside the variable, encoding scheme (for primitive type), and the container’s memory size.
  • An address is where a variable resides in the memory; it may be physical or logical.

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

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

If size and the starting address of a variable are known, then all the associated byte addresses with the variable are easy to calculate. The type of a variable defines its size. By established norms, a multi-byte variable is always stored in contagious locations.

🚉 Last Stop — Pointer

A Pointer is a variable, points to another variable. Formally, 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 Declaration

Declaration examples

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

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

Why we need pointers?

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

int first = a; // value of a is copied to first
int second = 2; // 2 is copied to second
Still, it is copying, but with pointer, the address is being copied.

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.

Run it →https://repl.it/repls/LikableIdolizedGoldfinc
pointer[i] = *(pointer + i)  (see next section)
Run it → https://repl.it/repls/WideeyedImpartialArrowworm

Thinking Mathematically

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

Pointer to Pointer example

Pointers 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.

new-location = old-location + number x sizeof(<type you declared>);
Run it → https://repl.it/repls/WideCoralVelvetworm

Dynamic allocation of memory

Pointers also enable us 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

Pointers to User Defined Types

A pointer can also point to user defined types: class, struct

// class : C++
Employee *pointerToEmployee = new Employee();
// calling member method
double salary = (*pointerToEmployee).getSalary();
// class : C++
Employee *pointerToEmployee = new Employee();
// calling member method
double salary = pointerToEmployee -> getSalary();
  • Null Pointer does not point to any memory location/variable.

Tip to reduce headaches

Wherever possible, visualize pointers graphically and deals with them mathematically to avoid unnecessary confusion and headaches.

Pointers Beyond C and C++

It is wrong to say that languages other than C and C++ do not support pointers. Most languages do support, but not extensively as C and C++ do.

  • In language GO, pointers have similar syntax and allowed operations (except pointer arithmetic).

Takeaways

  • Pointers provide a mechanism to indirectly access variables or functions.
  • An array name is a pointer to its first element.
  • * is used to declare pointer if used with type, and the same symbol is also used to de-reference the pointer.
  • Pointers can be added or subtracted, provided declared type is defined.
  • Pointers allow creating dynamic arrays.
  • -> operator is used to access type member: field or method.
  • A Null Pointer does not point to anything, and a Dangling pointer stores an invalid address.

Software Design Enthusiast. https://themightyprogrammer.dev