User:Sheetal singh

POINTERS

Pointers:-

Pointer is one of the key aspects of C++ language similar to that of C. Pointer offer a unique approach to handle data in C and C++. Pointer is a derived data type that refers to another data variable by storing the variable’s memory address rather than data. A pointer variable defines where to get the value of a specific data variable instead of defining actual data. Pointers provide an alternative approach to access other data objects.

Declaring and Initializing Pointers:- We can declare a pointer variable similar to other variables in C++. Like C, the declaration is based on the data type of the variable it points to. The declaration of a pointer variable takes the following form:

Here, pointer-variable is the name of the pointer, and the data-type refers to one of the valid C++ data types, such as int, char, float, and so on. The data-type is followed by an asterisk (*) symbol, which distinguishes a pointer variable from other variables to the compiler. A pointer variable can point to any type of data available in C++. A pointer is able to point to only one data type at the specific time. Let us declare and initialize a pointer variable as follows:

Here, ptr is a pointer variable and points to an integer data type. The pointer variable, ptr, should contain the memory location of any integer variable. The pointer variable, ptr, contains the address of the variable a. The second statement assigns the address of the variable a to the pointer ptr. The following program explains how to refer to a pointer’s address by using a pointer in C++.

The memory location is always addressed by the operating system. The output may vary depends on the system.

Manipulation of Pointers:-

We can manipulate a pointer with the indirection operator, i.e. ‘*’, which is also known as dereference operator. With this operator, we can indirectly access the data variable content. It takes the following general from:

As we know, dereferencing a pointer allows us to get the content of the memory location that the pointer points to. After assigning address of the variable to a pointer, we may want to change the content of the variable. Using the dereference operator, we can change the contents of the memory location. For example:

Pointer Expressions and Pointer Arithmetic:-

There are a substantial number of arithmetic operations that can be performed with pointers. C++ allows pointers to perform the following arithmetic operations: 	A pointer can be incremented (++) or decremented (--) 	Any integer can be added to or subtracted from a pointer 	One pointer can be subtracted from another Example:

In the above pointer variable, aptr, refers to the base address of the variable a. We can increment the pointer variable, shown as follows:

This statement moves the pointer to the next memory address. Similarly, we can decrement the pointer variable, as follows:

This statement moves the pointer to the previous memory address. Also, if two pointer variables point to the same array can be subtracted from each other. For example:

Using Pointers with Arrays and Strings:-

Pointer is one of the efficient tools to access elements of an array. Pointers are useful to allocate arrays dynamically, i.e. we can decide the array size at run time. Accessing an array with pointers is simpler than accessing the array index. In general, there are some differences between pointers and arrays, arrays refer to a block of memory space, whereas pointers do not refer to any section of memory. The memory addresses of arrays cannot be changed, whereas the content of the pointer variables, such as the memory addresses that it refer to, can be changed. We can declare the pointers to arrays as follows:

Or

Here, fptr points to the first element of the array of float, price [0]. Let us consider an example of using pointers to access an array of numbers and sum up the even numbers of the array.

Arrays of Pointers:-

An array of pointers point to an array of data items. Each element of the pointer array points to an item of data array. Data items can be accessed either directly or by dereferencing the elements of pointer array. We can reorganize the pointer elements without affecting the data items. We can declare an array of pointers as follows:

This statement declares an array of 10 pointers, each of which points to an integer. The address of the first pointer is inarray [0], and the second pointer is inarray [1], and the final pointer points to inarray [9]. Before initializing, they points to some unknown values in the memory space. We can use the pointer variable to refer to some specific values. The following program explains the implementation of array of pointers.

Pointers and Strings:-

Pointers are efficient to access two dimensional and multi-dimensional arrays in C++.There is definite relationship between arrays and pointers. C++ also allows us to handle the special kind of arrays, i.e. string with pointers.

We know that a string is one dimensional array of characters, which start with the index 0 and ends with the null character ‘\0’ in C++. A pointer variable can access a string by referring to its first character. As we know, there are two ways to assign a value to a string. We can use the character array or variable of type char *. Let us consider the following string declarations:

The first declaration creates an array of four characters, which contains the characters, ‘o’, ‘n’, ‘e’, ‘\0’, whereas the second declaration generates a pointer variable, which points to the first character, i.e. ‘o’ of the string. There is numerous string handling functions available in C++. All of these functions are available in the header file . In example, shows how to reverse a string using pointers and arrays.

Pointers to Functions:-

The pointer to function is known as callback function. We can use these function pointers to refer to a function. Using function pointers, we can allow a C++ program to select a function dynamically at run time. We can also pass a function as an argument to another function. Here, the function is passed as a pointer. The function pointers cannot be dereferencing. Like the other variables, we can declare a function pointer in C++. It takes the following form:

Example:

Remember that declaring a pointer only creates a pointer. It does not create actual function. The function must have the same return type and arguments. For example: