C plus plus Pointers

C++ Pointers

C++ Pointers are a special variable which stores addresses of other variable. Each pointers has a type of it points to, but one can freely cost between pointer types, although the behavior is implementation defined.
Pointers are declared and initialized as in C.

Int*ip;//in Pointer
Ip=&x;// address of X assigned to IP
*ip=10;//50 assigned to X through indirection
C++ adds the concept of constant pointer pointer to a constant.
Char * const ptr()=”GOOD”;//constant pointer

We cannot modify the address that ptrl is initialized to Int cost*ptr2= &m;//pointer to a constant
Ptr2 is declared as pointer to a constant. It can point to any variable as constants in the following way: Const char* const cp =”XYZ”;

This statement declares cp as a constant pointer to the string which has been declared a constant. In this c in case, neither the address assigned to the pointer cp nor the constant it points to can be changed.
Pointers are extensively used in C++ for memory management and achieving polymorphism.

C++ Pointer types

Many programs can be written in C++ without knowing pointers. However, pointers are very useful data type. There are too many things that can only be done with pointers.
C++ Pointers solve two common software problems. First, pointers allow different sections of the program to share information easily. You can get the same effect by copying information back and forth, but pointers solve the problem better. Second, pointers enable complex “linked” data structures like linked lists and binary trees.

What is a pointer?

Simple variables of type int and float store only values. An int variable is like a box which can store a single integer value. Simple variable can be represented as a box containing the value (As shown in following figure).

C++ Pointers
C++ Pointers

As pointer variable works in a different manner. It does not store the value directly. Instead, it stores a reference to this value. C++ Pointer variables can be represented as a box which contains the beginning of an arrow which leads to the variable it points to as shown in the above figure.
As shown in the above figure, the simple variable num contains the value 42 in the usual way. The variable numPtr is a pointer, which contains a reference to the variable num i.e., address of the variable num. The value stored in numPtr is a reference to an int.
The “deference” operation follows a pointer’s reference to get the value it points to. The value of the deference of numPtr in the previous example is 42. When the deference operation is used correctly, it’s simple. It just accesses the value pointed by the pointer variable. A C++ pointer must be assigned the address of a variable before deference operations will work.

C++ Null Pointer

The constant NULL is a special pointer value which means “points to nothing.” It is convenient to have a well-defined pointer value. It is a run time error to deference a NULL pointer. C++ NULL pointers can be represented as shown in following figure.

C++ Null Pointer
C++ Null Pointer

In C language, NULL is equal to the integer constant 0, so NULL can play the role of a Boolean false. But C++ no longer uses the NULL symbolic constant, use the integer constant 0 directly.

C++ Pointer declaration

A C++ pointer type is a type of the variable pointed to followed by an asterisk(*). The syntax for declaration of pointer variables is:
Type * pointer_name;
Where type is the type of type of data pointed, not the type of the pointer itself.
For e.g.,
Int*  type:pointer to int
Float*  type: pointer to float

Pointer variables are declared just like any other variable. The declaration gives the type and name of the new variable and reserves memory to hold its value. The declaration does not assign any address for the pointer-the pointer starts out with a junk value. When a pointer is first allocated, it does not have any reference. The pointer is “uninitialized” or simply “bad”. A deference operation on a bad pointer is a serious run time error. Sometimes, the bad pointer deference will corrupt a random area of memory, slightly altering the operation of the program so that it goes wrong any time later. Each pointer must be assigned a valid address before it can support deference operations. For e.g., int* numPtr;// This statement declares the int* (pointer to int) variable “numPtr”. This allocates space for the pointer. The pointer starts out “bad”.

Pointer initialization

When declaring C++ pointers we can explicitly specify to which variable we want them to point. This mechanism is called Pointer Initialization. For e.g.,
Int number;
Int *x = &number;
This is equivalent to:
Int number;
Int *x;
X = &number;
Similar to arrays, the compiler allows to initialize the content at which the pointer points along with declaring the variable pointer.
Char *mesg =”hello”;
Here, the static storage is reserved for containing “hello” and a pointer to the first char of this memory block (that corresponds to ‘h’) is assigned to mesg. The pointer mesg points to a string of characters and can be used exactly as if it was an array (array is just a constant pointer).
To replace the ‘o’ by ‘!’ sign in the content pointed by mesg. We can do it by any of the following two ways:
Because mesg[4] is just the  same as to write *(mesg+4), although the most usual expression is the first one.

Leave a Reply

Your email address will not be published.