Pointer Derefference

Simplest pointer derefference

Create a simple program that demonstrates the use of pointers with integer variables. The program should declare an integer variable, declare a pointer to the integer variable, and print the variable's value using the pointer.

Hint

Use int* to declare a pointer to an int. Use *pointer to derefference (get a refference to what the pointer points to) a pointer.

Answer

#include <iostream>

int main() {
    // Step 1: Declare an integer variable and assign it a value
    int myNumber = 42;

    // Step 2: Declare a pointer to the integer variable
    int* myPointer = &myNumber;

    // Step 3: Print the value of the integer variable using the pointer
    std::cout << "Value of myNumber: " << myNumber << std::endl;
    std::cout << "Value of myNumber using the pointer: " << *myPointer << std::endl;

    return 0;
}
Questions