Pointers are declared and initialized as in C.
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”;
Pointers are extensively used in C++ for memory management and achieving polymorphism.
C++ Pointer types
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).
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.
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.
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”.
Int *x = &number;
This is equivalent to:
X = &number;
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 is just the same as to write *(mesg+4), although the most usual expression is the first one.