×
>
<

C++ Programming

C++ Pointers | CrackEase

C++ Pointer

What is a Pointer

A pointer in C++ is a variable that stores the memory address of another variable. A pointer can refer to objects of any type (int, char, arrays, functions, or even other pointers). The size of a pointer depends on the target architecture (for example, 4 bytes on a typical 32-bit system and 8 bytes on a typical 64-bit system).

Declaring a Pointer

Declare a pointer using the asterisk * (indirection) operator.

Declaration
int *x; // pointer to int  
char *y; // pointer to char      

Use & (address-of) to get an object's address and * to dereference a pointer and access the value at that address.

Example :

#include <iostream>
using namespace std;

int main()
{
    int number = 15;
    int *p = &number; // stores the address of number
    cout << "Address of number variable is: " << &number << endl;
    cout << "Address stored in pointer p is: " << p << endl;
    cout << "Value pointed to by p is: " << *p << endl;
    return 0;
}

Sample output (addresses will vary):

Address of number variable is: 0x7ffccc8724c4
Address stored in pointer p is: 0x7ffccc8724c4
Value pointed to by p is: 15
Facts About Pointer
pointer facts

Advantages

  • Pointers enable efficient manipulation of arrays, strings, trees and other data structures and can improve performance.
  • They allow functions to modify variables passed by reference and enable returning multiple results (via out-parameters).
  • Pointers make dynamic memory management and low-level memory access possible.

Uses of Pointer

1) Dynamic memory allocation
In C++ you allocate memory dynamically using operators like new (and in C with malloc()), and pointers are used to refer to that memory.

2) Arrays, Functions, and Structures
Pointers are heavily used with arrays, function pointers and structures to write flexible and efficient code.

What is a pointer to a pointer?
In C++ you can have multiple levels of indirection — a pointer that points to another pointer. Each level uses an additional * in the declaration.
char a;
char *b;
char **c;
a = 'g';
b = &a;
c = &b;
Types of Pointers
Common pointer types:
  • Null Pointer
  • Void Pointer
  • Wild Pointer (uninitialized)
  • Dangling Pointer

1. Null Pointer

A null pointer points to nothing and is typically initialized with nullptr (C++11+) or NULL in older code.
Syntax :
int *var = nullptr;
Safe usage example :
#include <iostream>
using namespace std;

int main()
{
    int *var = nullptr;
    if (var) {
        cout << "var = " << *var << endl;
    } else {
        cout << "var is a null pointer, do not dereference" << endl;
    }
    return 0;
}
var is a null pointer, do not dereference

2. Void Pointer

A void* can hold the address of any data type, but you must cast it back to the correct type before dereferencing.
Syntax :
void *var;
Example :
#include <iostream>
using namespace std;

int main()
{
    int a = 2;
    void *ptr = &a;
    // Cast back to int* before dereferencing:
    cout << "After typecasting, a = " << *static_cast<int*>(ptr) << endl;
    return 0;
}
Sample output :
After typecasting, a = 2

3. Wild Pointer

A wild pointer is an uninitialized pointer that may hold any address — dereferencing it causes undefined behaviour (often a crash). Always initialize pointers.
Bad example (do NOT do this):
#include <iostream>
using namespace std;

int main()
{
    int *ptr;            // uninitialized — wild pointer
    // cout << "ptr = " << *ptr; // undefined behaviour / likely crash
    return 0;
}
Safe practice :
int *ptr = nullptr; // initialize to nullptr until assigned a valid address

4. Dangling Pointer

A dangling pointer points to memory that has been deallocated. Accessing it is undefined behaviour. Avoid returning pointers to local (stack) variables or dereferencing after free/delete.
Bad pattern (illustration):
#include <iiostream>
#include <cstdlib>
using namespace std;

int main()
{
    int *ptr = (int*)malloc(sizeof(int)); // allocate
    *ptr = 10;
    free(ptr); // memory freed
    // Now ptr is a dangling pointer — dereferencing it is undefined
    // cout << *ptr; // do NOT do this
    return 0;
}
Safe approach :
free(ptr);
ptr = nullptr; // reset so it is no longer dangling
(If you dereference after free you may get runtime errors such as segmentation fault or heap corruption.)
Footer Content | CrackEase