The use of pointer 2

#include <iostream>
#include <stdio.h>
#include <fstream>
using namespace std;
//pointer_2.cpp
//size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
int main()
{
int * p1, * p2;
int n1=20, n2=50;
p1 = &n1;
// *p1 = 20;
p2 = &n2;
// *p2 = 50;
cout <<"Value of p1 " << *p1 <<endl ;
cout<< "Value of p2 " << *p2 <<endl;
cout << "value of p1 " << p1 << " Value of p2 " << p2 <<endl;
cout <<"\address of n1 " << &n1 <<endl;
cout<< "\address of p1 " << p1;
return 0;
}

In the above example, if you don't point to the the variable p1. it prints out its memory location.
you may alos use a pointer like this
  • int* p1;

#include <iostream>
#include <stdio.h>
#include <fstream>
using namespace std;
//pointer_3.cpp
//size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
int main()
{
int* p1, * p2;
int n1=20, n2=50;
p1 = &n1;
// *p1 = 20;
p2 = &n2;
// *p2 = 50;
cout <<"Value of p1 " << *p1 <<endl ;
cout<< "Value of p2 " << *p2 <<endl;
cout << "value of p1 " << p1 << " Value of p2 " << p2 <<endl;
cout <<"\address of n1 " << &n1 <<endl;
cout<< "\address of p1 " << p1;
return 0;
}