GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||
GeezerVison is showing www.geeksforgeeks.org — C++ Pointers - GeeksforGeeks C++ PointersLast Updated : 21 May, 2026 A pointer is a special variable that holds the memory address of another variable, rather than storing a direct value itself. Pointers allow programs to access and manipulate data in memory efficiently, making them a key feature for system-level programming and dynamic memory management. When we access a pointer directly, we get the address it holds not the actual data stored at that location.
Output Value of x: 10 In the code above: ![]()
So, ptr holds the address of x, and *ptr gives the value of x by accessing that address. Create PointerA pointer can be declared in the same way as any other variable but with an asterisk symbol (*) as shown:
Here, data_type is the type of data that the pointer is pointing to, and name is the name assigned to the pointer. The * symbol is also called dereference operator. Example:
In the above statement, we create a pointer ptr that can store the address of an integer data. It is pronounced as "Pointer to Integer" or "Integer Pointer" Assign AddressThe addressof operator (&) determines the address of any variable in C++. This address can be assigned to the pointer variable to initialize it. Example:
In the above statement, pointer ptr store the address of variable val using address-of operator (&). The pointer and the variable should be of same type, Otherwise type mismatch error occurs. DereferencingThe process of accessing the value present at the memory address pointed by the pointer is called dereferencing. This is done with the help of dereferencing operator as shown:
Output 10 Directly accessing the pointer will just give us the address that is stored in the pointer.
Output 0x7fffa0757dd4Modify AddressThe pointer can be modified to point to another memory address if its is of the same type. Example:
Output 10 99 Size of PointersThe size of pointer in a system is equal for every pointer no matter what type of data it is pointing to. It does not depend on the type, but on operating system and CPU architecture. The size of pointers in C++ is
The logic is simple: pointers store the addresses of the memory and in a computer, the maximum width of a memory address is determined by the CPU architecture. For example, for a 64-bit CPU, the address will always be 64-bit wide. This can be verified using sizeof operator.
Output 8 8 As we can see, both the pointers have same size. It's a 64-bit system, so the size is 8 bytes. Special Types of PointersThere are 4 special types of pointers that used or referred to in different contexts: Wild PointerWhen a pointer is created, it just contains a random address that may or may not be valid. This type of pointer is called wild pointer.
Dereferencing this pointer may lead to errors such as segmentation faults. So, it is always recommended to initialize a pointer. NULL Pointer (nullptr)A nullptr is a pointer that does not point to any valid memory location. It is often used to initialize a pointer when you do not want it to point to any object.
Void PointersA void pointer (void*) is a special type of pointer in C++ that has no associated data type. It can hold the address of any data type, making it useful for generic programming. However, since the type is unknown, the compiler doesn't know how many bytes to read or how to interpret the data. Therefore, a void pointer cannot be directly dereferenced. It must first be explicitly typecast to the appropriate pointer type.
Output Value pointed by void pointer: 42 Dangling PointerA dangling pointer is a pointer that refers to memory which has already been freed or is no longer valid. This typically happens when:
Dereferencing a dangling pointer leads to undefined behavior, and is a common source of hard-to-find bugs.
Pointer ArithmeticPointer arithmetic refers to the operations that C++ allows on the pointers. They include:
Pointer To Pointer (Double Pointer)We can also have pointers that point to other pointers. This is called a double pointer, and it is useful when working with multi-level data structures like arrays of pointers or dynamic memory management. Example:
Try It Yourself Output 10 10 Pointer to FunctionsIn C++, a function pointer is used to point to functions, similar to how pointers are used to point to variables. It allows you to save the address of a function. Function pointers can be used to call a function indirectly, or they can be passed as arguments to another function, enabling dynamic function invocation and flexibility in function handling. Smart PointersA smart pointer is a wrapper class around a pointer that overloads operators like * and ->. Smart pointer objects resemble normal pointers, they have the added functionality of automatically deallocating and freeing the memory of the object when it is no longer needed, unlike regular pointers. Pointer vs ReferenceUnderstanding the differences between pointers and references in C++. Both are used to refer to other variables, but they operate in different ways and have different behavior.
Uses of PointersPointers are a useful concept in C++ that allow direct access to memory addresses, providing greater control over memory and data manipulation. Below are some primary uses of pointers in C++:
Comment ExploreStandard Template Library(STL) Practice & Problems | ||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |