ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Programming Fundamentals
    COMP1112
    Progress0 / 19 topics
    Topics
    1. Introduction to Problem Solving2. Von-Neumann Architecture3. Introduction to Programming4. Role of Compiler and Linker5. Introduction to Algorithms6. Basic Data Types and Variables7. Input/Output Constructs8. Arithmetic, Comparison and Logical Operators9. Conditional Statements and Execution Flow10. Repetitive Statements and Execution Flow11. Lists and Memory Organization12. Multi-dimensional Lists13. Introduction to Modular Programming14. Function Definition and Calling15. Stack Rolling and Unrolling16. Strings and String Operations17. Pointers/References18. Static and Dynamic Memory Allocation19. File I/O Operations
    COMP1112›Pointers/References
    Programming FundamentalsTopic 17 of 19

    Pointers/References

    4 minread
    603words
    Beginnerlevel

    Pointers and References in C++

    Pointers and references are fundamental concepts in C++ that allow for efficient memory management and data manipulation. Understanding these concepts is crucial for effective programming, as they provide powerful capabilities for working with data.

    1. Pointers

    A pointer is a variable that stores the memory address of another variable. Pointers are used for dynamic memory allocation, array manipulation, and efficient function parameter passing.

    1.1. Declaration and Initialization

    To declare a pointer, you use the asterisk (*) symbol:

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

    You can initialize a pointer by assigning it the address of a variable using the address-of operator (&):

    int x = 10;
    int* ptr = &x; // ptr now points to x
    

    1.2. Dereferencing

    To access the value stored at the address a pointer is pointing to, you use the dereference operator (*):

    int value = *ptr; // Gets the value of x through ptr
    

    1.3. Pointer Arithmetic

    Pointers can be incremented or decremented, which changes the address they point to based on the size of the data type:

    int arr[] = {10, 20, 30};
    int* ptr = arr; // Points to the first element
    ptr++; // Now points to the second element (20)
    

    1.4. Dynamic Memory Allocation

    Pointers are commonly used with dynamic memory allocation using new and delete:

    int* ptr = new int; // Allocates memory for an integer
    *ptr = 5; // Assigns a value to the allocated memory
    delete ptr; // Frees the allocated memory
    

    2. References

    A reference is an alias for an existing variable. Once a reference is established, it cannot be changed to refer to another variable. References provide a way to access variables without using pointers, making them safer and easier to use.

    2.1. Declaration and Initialization

    You declare a reference using the ampersand (&) symbol:

    int x = 10;
    int& ref = x; // ref is now a reference to x
    

    2.2. Accessing Values

    You can use the reference just like the original variable:

    ref = 20; // Changes x to 20
    

    2.3. Passing by Reference

    References are often used to pass variables to functions without making a copy of the variable, allowing for efficient modifications:

    void increment(int& value) {
        value++; // Increments the original variable
    }
    
    int main() {
        int x = 5;
        increment(x); // x is now 6
        return 0;
    }
    

    3. Key Differences Between Pointers and References

    Feature Pointers References
    Syntax Uses * for declaration Uses & for declaration
    Nullability Can be null Cannot be null
    Reassignment Can be reassigned to point to another variable Cannot be reassigned
    Dereferencing Requires * to access value Accessed directly
    Size Size varies (usually 4 or 8 bytes) Same size as the original type

    4. When to Use Pointers vs. References

    • Use Pointers:

      • When you need to allocate memory dynamically.
      • When you want to manage arrays or perform pointer arithmetic.
      • When a null value is meaningful (e.g., indicating no object).
    • Use References:

      • When you want to create aliases for existing variables.
      • When passing large objects to functions to avoid copying.
      • When you need to ensure that a variable must be referenced.

    Summary

    Pointers and references are essential tools in C++ that enhance your ability to manipulate data and memory. Pointers provide flexibility and power for memory management and data manipulation, while references offer a safer and easier way to work with variables. Mastering both concepts is crucial for effective programming in C++.

    Previous topic 16
    Strings and String Operations
    Next topic 18
    Static and Dynamic Memory Allocation

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time4 min
      Word count603
      Code examples0
      DifficultyBeginner