Skip to content

A Basic Pointer Example

#include <iostream>
#include <cstdlib>

int squareValue(int *value);

int main() {
    int number = 5;
    int *numberPtr = &number;

    std::cout << "number is " << *numberPtr << std::endl;
    std::cout << "If use it in a method " << std::endl;
    int squaredNumber = squareValue(&number);
    std::cout << "The square of number is " << squaredNumber << std::endl;
    std::cout << "The number after squaring is " << number << std::endl;

    std::cout << "Press enter to continue...";
    std::cin.ignore();

    return EXIT_SUCCESS;
}

int squareValue(int *value) {
    int squared = (*value) * (*value);
    return squared;
}

Oh hi there 👋 It’s nice to meet you.

Sign up to receive awesome content in your inbox, every month.

We don’t spam!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.