Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What Are Pointers and How They Work
Computer Science

What Are Pointers and How They Work

Memory and pointers in C++

Ihor Gudzyk

by Ihor Gudzyk

C++ Developer

Oct, 2024
7 min read

facebooklinkedintwitter
copy
What Are Pointers and How They Work

Pointers can be challenging to understand at first, but they are straightforward on their own. The difficulty usually arises from their use cases, especially when multiple pointers are involved.

Introduction to Pointers

A pointer is a variable that stores the memory address of another variable. Rather than holding a direct value, it points to a location in memory where that value resides.

Imagine someone asks to borrow your class notes, but you want to keep the originals for yourself. So, instead, you make a copy and give them that version. This is exactly what happens when you pass a variable to the function.

Alternatively, you could simply give the person a path to the location where you’ve hidden your notes. This way, you don't need to create a copy. However, if the person isn’t careful with your notes, they could get damaged or even destroyed. The function analogy for this case would be:

As you can see you use pointers in that case. Pointers offer several benefits, especially in system-level and embedded programming.

How Pointers Work

Pointers involve two fundamental concepts: address and dereferencing. Each variable occupies a specific memory address in the computer. A pointer can store the address of a variable, and dereferencing allows access to the value stored at that address:

  • address of Operator (&): Returns the address of a variable;
  • dereference Operator (*): Accesses the value at the address held by the pointer.

Run Code from Your Browser - No Installation Required

Run Code from Your Browser - No Installation Required

Pointer Arithmetic

In pointer arithmetic, adding or subtracting from a pointer shifts it to the next or previous memory location based on the data type it points to.

Array and Pointers

Arrays and pointers are closely related. The name of an array acts as a pointer to its first element. Arrays are created by arranging elements in contiguous memory blocks, with each element stored in consecutive memory locations. This layout allows pointer arithmetic to access any element within the array enabling even some unconventional tricks like this:

Types of Pointers

There are several types of pointers, each serving a specific purpose. Take a look at some common ones:

Null PointerPoints to nothing (NULL).int *p = NULL;
Void PointerCan point to any data type.void *ptr;
Dangling PointerPoints to memory that has been deallocated.free(ptr);
Function PointerPoints to the address of a function.void (*funcPtr)();
Array PointerPoints to the first element of an array.int *arrPtr = &arr[0];

Start Learning Coding today and boost your Career Potential

Start Learning Coding today and boost your Career Potential

FAQs

Q: What is the difference between a pointer and a reference?
A: A reference is an alias for an existing variable, while a pointer holds the memory address of another variable. Pointers offer more flexibility but require explicit management.

Q: Can I store the address of a constant in a pointer?
A: Yes, but you must declare the pointer as a pointer to a constant: const int *p = &a;. This prevents modification of the value through the pointer.

Q: What happens if I dereference a NULL pointer?
A: Dereferencing a NULL pointer causes undefined behavior and can crash the program. Always check if a pointer is NULL before using it.

Q: Why do we use malloc in C but new in C++?
A: malloc is used in C for memory allocation, while new is used in C++ because it also calls constructors for object initialization.

Q: How can I avoid memory leaks when using pointers?
A: Always pair each malloc or new with free or delete. In C++, using smart pointers like std::unique_ptr or std::shared_ptr helps manage memory automatically.

Was this article helpful?

Share:

facebooklinkedintwitter
copy

Was this article helpful?

Share:

facebooklinkedintwitter
copy

Content of this article

We're sorry to hear that something went wrong. What happened?
some-alt