C++ Pointers

C++ Pointers

The pointer in C++ language is a variable, it is also known as locator or indicator that points
to an address of a value.
Cpp Pointers 1Advantage of pointer
1) Pointer reduces the code and improves the performance, it is used to retrieving strings,
 trees etc. and used with arrays, structures and functions.
2) We can return multiple values from function using pointer.
3) It makes you able to access any memory location in the computer's memory.
Usage of pointer
There are many usage of pointers in C++ language.
1) Dynamic memory allocation
In c language, we can dynamically allocate memory using malloc() and calloc() functions where
pointer is used.
2) Arrays, Functions and Structures
Pointers in c language are widely used in arrays, functions and structures. It reduces the code
and improves the performance.

C++ pointers are easy and fun to learn. Some C++ tasks are performed more
 easily with pointers, and other C++ tasks, such as dynamic memory allocation,
 cannot be performed without them.
As you know every variable is a memory location and every memory location 
has its address defined which can be accessed using ampersand (&) operator
 which denotes an address in memory. Consider the following which will print 
the address of the variables defined −
#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;
}
When the above code is compiled and executed, it produces the following result −
Address of var1 variable: 0xbfebd5c0
Address of var2 variable: 0xbfebd5b6

What are Pointers?

pointer is a variable whose value is the address of another variable. Like any 
variable or constant, you must declare a pointer before you can 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
The actual data type of the value of all pointers, whether integer, float, character,
 or otherwise, is the same, a long hexadecimal number that represents a memory 
address. The only difference between pointers of different data types is the data
 type of the variable or constant that the pointer points to.

Using Pointers in C++

There are few important operations, which we will do with the pointers very
 frequently. (a) We define a pointer variable. (b) Assign the address of a variable
 to a pointer. (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 −
#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;
}
When the above code is compiled and executed, it produces result something as
 follows −
Value of var variable: 20
Address stored in ip variable: 0xbfc601ac
Value of *ip variable: 20


1 comment:

  1. Hello There,

    Your blog is such a complete read. I like your approach with C++ Pointers. Clearly, you wrote it to make learning a cake walk for me.

    I want to make a dynamic array of char* returned by a function. I need to save the name of files contained inside a folder to a dynamic array of char*.
    My IDE is dev-c++, my favorite, my language is C.

    Awesome! Thanks for putting this all in one place. Very useful!

    Thanks a heaps,
    Preethi

    ReplyDelete