Sobes.tech
Junior — Middle

Can you explain what a variable that stores the address of another object in memory is?

sobes.tech AI

Answer from AI

A variable that stores the address of another object in memory is called a pointer. In C and C++ languages, a pointer contains the memory address where another object (such as a variable, array, or structure) is located. This allows indirect access to data, modification of it, or passing large objects by reference without copying.

Example of using a pointer in C++:

#include <iostream>

int main() {
    int a = 10;
    int* p = &a; // p stores the address of variable a

    std::cout << "Value of a: " << a << std::endl;          // 10
    std::cout << "Address of a: " << p << std::endl;        // memory address
    std::cout << "Value at address p: " << *p << std::endl; // 10

    *p = 20; // change the value of a through the pointer
    std::cout << "New value of a: " << a << std::endl;     // 20

    return 0;
}

Thus, pointers provide flexibility in memory management and enable the creation of complex data structures.