Tuesday, 8 August 2017

Write and Explain a simple program in C++ using pointers

Write and Explain a simple program in C++ using pointers:

What is Pointer ?

In computer science, a pointer is a programming language object, whose fee refers to (or "points to") some other value saved someplace else in the computer memory the use of its memory address. A pointer references a location in memory, and acquiring the fee saved at that region is called dereferencing the pointer.

So we can say that C++ pointer are smooth and fun to study. Some C++ tasks are executed more effortlessly with pointers, and other C++ tasks, along with dynamic memory allocation, can't be completed without them.

Explain:
As you already know each variable is a memory location and every memory location has its address defined which may be accessed using ampersand (&) operator which denotes an address in memory. Consider the following on the way to print the address with of the variables defined:

Code:

#include <iostream>
using namespace std;

int main ()
 {
   int  var1;
   char var2[10];
   cout << "Address of var1 variable: ";
   cout << &var1 << endl;
   cout << "Address of var2 variable: ";
   cout << &var2 << endl;
   return 0;
}

Output:


Now here is pointers:

A pointer is a variable whose value is the address with of every other variable. Like any variable or constant, you must declare a pointer earlier than you may work with it. The general form of a pointer variable declaration is:

type *var-name;


Here, type is the pointer's base type; it must be a valid C++ type and var-name is the name of the pointer variable. The asterisk you used to declare a pointer is the same asterisk that you use for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer. Following are the valid pointer declaration:

int    *ip;    // pointer to an integer
double *dp;    // pointer to a double
float  *fp;    // pointer to a float
char   *ch     // pointer to character

Now Use Of Pointer In C++ :

There are few important operations, which we will do with the pointers very frequently. (a) we define a pointer variables (b) assign the address of a variable to a pointer and (c) finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand. Following example makes use of these operations:


Code:

#include <iostream>
using namespace std;

int main () {
   int  var = 20;   // actual variable declaration.
   int  *ip;        // pointer variable 
   ip = &var;       // store address of var in pointer variable
   cout << "Value of var variable: ";
   cout << var << endl;
   // print the address stored in ip pointer variable
   cout << "Address stored in ip variable: ";
   cout << ip << endl;
   // access the value at the address available in pointer
   cout << "Value of *ip variable: ";
   cout << *ip << endl;
   return 0;
}

Output:














Try it yourself.
If you have any problem you can tell me on comment box i will help you.
thanks.

Viral Programs

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

1 comments:

  1. very cool content and very helping content please keep updating

    ReplyDelete