Showing posts with label Pointers. Show all posts
Showing posts with label Pointers. Show all posts

Thursday, November 6, 2014

How to change value of any declared and initialized integer by pointer?

Pointer is a powerful tool in C++. Pointer is a variable which contains address of variable. Let us declare 
a=30;
If we want to change the value of a then we usually type this code ;
a=40;
But using pointer we can change the value of a through its memory location without disturbing a this is the main thing that pointer enable us to make changes.
Here is an example.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int a=50; //declare and initialize a
    cout<<"The value of a before "<<a;
    int *b; //declare pointer
    b=&a;   //pass address of a to b
    *b=100; //change value of a by pointer
    cout<<"\nThe value of a after changing "<<a;
getch();
}

The output of above program is :

Thursday, June 5, 2014

C++ Program to print heart shape using pointer

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char x=3;
char *a;
a=&x;
cout<<"\I\t"<<*a<<"\tProgramming";
getch();
return 0;
}


Sunday, June 1, 2014

C++ Simple pointer program to print address and value

#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
     int a= 5;
 /* actual variable declaration */
 int *ptr;
 /* pointer variable declaration */
  ptr = &a;
   /* store address of a in pointer variable*/
   cout<<"Address of variable a="<< &a;
    cout<<"\nAddress stored in variable ptr="<<ptr;
     cout<<"\nValue of *ptr variable="<<*ptr;
     getch();
     return 0;
      }