JKreft
0
Q:

how to use pointes

#include <iostream>
using namespace std;

int main () {

    int firstvalue = 5, secondvalue = 15;
    //declaring pointers
    int  *p1, *p2;

    p1 = &firstvalue;       // p1 = address of firstvalue
    p2 = &secondvalue;      // p2 = address of secondvalue
    *p1 = 40;               // value pointed to by p1 = 10;
    *p2 = *p1;              // value pointed to by p2 = value pointed to by p1
    p1 = p2;                // p1 = p2 (value of pointer is copied)
    *p1 = 20;               // value pointed to by p1 = 20;
    //p1 got replaced with the address of p2;
    cout << "firstvalue is " << firstvalue << '\n';
    cout << "secondvalue is " << secondvalue << '\n';



    return 0;
}
0

New to Communities?

Join the community