top of page

What is a Pointer?

  • Writer: Siddharth Sharma
    Siddharth Sharma
  • Nov 7, 2024
  • 2 min read

A pointer is a variable that stores the memory address of another variable rather than its actual value. Pointers are fundamental in programming languages like C and C++ and are widely used to manage memory, enable efficient data manipulation, and pass variables by reference.


Defining a Pointer

In C/C++, a pointer is defined by using the * symbol. Here’s an example:


int* ptr;  // ptr is a pointer to an integer

In this example:

  • int* ptr; declares ptr as a pointer to an int.

  • The * in int * indicates that ptr will hold the address of an integer.


Initializing a Pointer

To use a pointer effectively, you must initialize it by assigning it the address of a variable. The address-of operator (&) is used to retrieve the memory address of a variable.


int num = 10;      // Regular integer variable
int* ptr = #   // ptr now holds the address of num

Here:

  • num is a regular integer variable with the value 10.

  • &num gives the memory address of num, and ptr holds this address.


Dereferencing a Pointer

To access the value at the address a pointer holds, we use the dereference operator *. Dereferencing a pointer allows us to access or modify the value of the variable at that address.


printf("%d", *ptr);  // Outputs the value at the memory address stored in ptr (which is 10 in this case)

Uses of Pointers

Pointers are highly versatile and offer several key uses in programming:

  1. Dynamic Memory Allocation: Using functions like malloc, calloc, and free in C, and new and delete in C++, pointers enable efficient management of memory at runtime.

  2. Passing by Reference: Functions can use pointers to modify the values of variables in the calling scope, enabling more efficient parameter passing.

  3. Efficient Data Structures: Pointers are fundamental to building complex data structures like linked lists, trees, and graphs.

  4. Function Pointers: C/C++ allows pointers to functions, enabling the dynamic selection and execution of functions.

  5. Array and String Manipulation: Pointers allow direct manipulation of arrays and strings, making operations more efficient and versatile.



Example of Pointer Usage

Here’s an example demonstrating the basics of pointer usage:


#include <stdio.h>

int main() {
    int num = 20;      // Initialize an integer variable
    int* ptr = &num;   // Initialize a pointer to hold the address of num

    // Display the value and address of num using the pointer
    printf("Value of num: %d\n", num);
    printf("Address of num: %p\n", (void*)ptr);
    printf("Value at ptr: %d\n", *ptr);

    // Modify num through the pointer
    *ptr = 30;
    printf("Modified value of num: %d\n", num);

    return 0;
}

Explanation of the Code:

  • int* ptr = &num; initializes a pointer ptr to hold the address of the integer num.

  • *ptr = 30; modifies the value of num by dereferencing ptr, setting it to 30.


Output:

Value of num: 20
Address of num: [some memory address]
Value at ptr: 20
Modified value of num: 30

In this example:

  • We use *ptr = 30 to modify num indirectly via its pointer. This is useful for in-place modifications without copying data.

 
 
 

Comments


bottom of page