ITclub

العودة إلى C++
محاضرة 7

Pointers in C++

المؤشرات (Pointers): التعريف، الاستخدام، والعمليات الأساسية

ملخص المحاضرة

📜 Lecture 7: Pointers in C++

This lecture introduces one of the most important and powerful concepts in C++: Pointers.

Key Concepts

  • What is a Pointer?: A pointer is a variable that stores the memory address of another variable.
    • Instead of holding a value like 10 or 'A', a pointer holds an address like 0x7ffeefbff5bc.
  • Why Use Pointers?:
    • Efficient memory management
    • Pass by reference to functions
    • Dynamic memory allocation
    • Working with arrays and strings
    • Building complex data structures

Pointer Declaration

  • Syntax: data_type* pointer_name; or data_type *pointer_name;
  • Examples:
    int* ptr; // Pointer to an integer double* dptr; // Pointer to a double char* cptr; // Pointer to a character

The Address-of Operator (&)

  • Purpose: Gets the memory address of a variable.
  • Syntax: &variable_name
  • Example:
    int x = 10; int* ptr = &x; // ptr now holds the address of x

The Dereference Operator (*)

  • Purpose: Accesses the value stored at the address a pointer points to.
  • Syntax: *pointer_name
  • Example:
    int x = 10; int* ptr = &x; cout << *ptr; // Prints 10 (the value of x) *ptr = 20; // Changes x to 20 cout << x; // Prints 20

Important Concepts

  • Pointer vs Variable:

    • x → the value stored in variable x
    • &x → the address of variable x
    • ptr → the address stored in the pointer
    • *ptr → the value at the address the pointer points to
  • NULL Pointer: A pointer that doesn't point to any valid memory location.

    int* ptr = NULL; // C-style int* ptr = nullptr; // C++11 style (recommended)
    • Always initialize pointers to NULL/nullptr if you don't have a valid address yet.
    • Helps avoid crashes from accessing random memory.

Pointers and Arrays

  • Key Relationship: The name of an array is actually a pointer to its first element.
  • Example:
    int arr[3] = {10, 20, 30}; int* ptr = arr; // ptr points to arr[0] cout << *ptr; // Prints 10 (arr[0]) cout << *(ptr + 1); // Prints 20 (arr[1]) cout << *(ptr + 2); // Prints 30 (arr[2])
  • Pointer Arithmetic: You can add/subtract integers to move through array elements.

Pass by Reference

  • Without Pointers (Pass by Value): Changes inside the function don't affect the original variable.
  • With Pointers (Pass by Reference): You can modify the original variable through the pointer.
  • Example:
    void increment(int* ptr) { (*ptr)++; // Increments the value at the address } int main() { int x = 5; increment(&x); // Pass the address of x cout << x; // Prints 6 }

Common Mistakes to Avoid

  1. Uninitialized Pointers: Always initialize pointers before using them.

    int* ptr; // BAD: points to random memory *ptr = 10; // CRASH! int* ptr = nullptr; // GOOD: safe initialization
  2. Dereferencing NULL: Check before dereferencing.

    if (ptr != nullptr) { cout << *ptr; }
  3. **Confusing & and ***: Remember:

    • & gets the address
    • * gets the value at an address

Quick Reference

ExpressionMeaning
int* ptr;Declare a pointer to int
ptr = &x;ptr stores the address of x
*ptrAccess the value at the address ptr points to
&xGet the address of variable x
ptr = nullptr;Set pointer to NULL
*(ptr + i)Access array element (same as ptr[i])