C++ Pointers
π C++ Pointers
A pointer in C++ is a variable that stores the memory address of another variable.
Pointers are essential for dynamic memory, arrays, functions, and low-level programming.
πΉ 1. Pointer Declaration
intβ type of data it points to*β pointer symbolptrβ pointer variable
πΉ 2. Pointer Initialization
&xβ address ofxptrstores that address
πΉ 3. Access Value Using Pointer (Dereferencing)
ptrβ address*ptrβ value at that address
πΉ 4. Change Value Using Pointer
πΉ 5. Pointer and Array Relationship
πΉ 6. Pointer Arithmetic
β‘ Pointer moves by sizeof(type) bytes.
πΉ 7. Null Pointer
β Safe initialization
β Avoids garbage address
πΉ 8. Pointer in Functions (Call by Reference)
πΉ 9. Pointer vs Reference
| Pointer | Reference |
|---|---|
| Can be NULL | Cannot be NULL |
| Can be reassigned | Cannot be reassigned |
| Needs dereferencing | No dereferencing |
| More flexible | Safer & simpler |
πΉ 10. Common Pointer Types
Pointer to Pointer
Constant Pointer
Pointer to Constant
β Common Mistakes
β Correct:
π Summary
Pointer stores a memory address
&β get address,*β access valueUsed with arrays, functions, dynamic memory
Must be initialized properly
