pointer in c++

GOCOURSE
Pointers in c++

Pointer: 

A pointer is a variable in C++ that stores another variable's memory address. Because they enable us to directly alter memory, pointers are powerful programming tools.

Example:

#include <iostream>
using namespace std;

int main() {
    int num = 10;
    int *ptr;

    ptr = &num; // assign the address of num to ptr

    cout << "Value of num is: " << num << endl;
    cout << "Address of num is: " << &num << endl;
    cout << "Value of ptr is: " << ptr << endl;
    cout << "Value pointed to by ptr is: " << *ptr << endl;

    *ptr = 20; // change the value of num through ptr

    cout << "Value of num is now: " << num << endl;
    return 0;
}

Output:

Value of num is: 10
Address of num is: 0x7ffd0589538c
Value of ptr is: 0x7ffd0589538c
Value pointed to by ptr is: 10
Value of num is now: 20

Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send