ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP1112Pointers References
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP1112

    Programming Fundamentals

    Progress0 / 19 done
    Topics19
    1Introduction to Problem Solving2Von-Neumann Architecture3Introduction to Programming4Role of Compiler and Linker5Introduction to Algorithms6Basic Data Types and Variables7Input/Output Constructs8Arithmetic, Comparison and Logical Operators9Conditional Statements and Execution Flow10Repetitive Statements and Execution Flow11Lists and Memory Organization12Multi-dimensional Lists13Introduction to Modular Programming14Function Definition and Calling15Stack Rolling and Unrolling16Strings and String Operations17Pointers/References18Static and Dynamic Memory Allocation19File I/O Operations
    4 min read
    Topic 17 of 19
    Topic Study Note Illustration
    17

    Pointers/References

    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...

    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 view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    603words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Programming Fundamentals