C++ Pointers and how to use them in c++ programming

C++ Pointers

C++Pointers:

C++ pointers are one of the most powerful elements of the C/C++ language. It provides a way of accessing the variable. A pointer is a special variable that represents the location or address of another variable (rather than the value of a variable). Knowing the address of a variable enables you to set and query its value.

As you known that C/C++ is a mid-level language. It contains the low-level features as well. In low-level programming, we deal with the address of data frequently; for this pointers play a tremendous role.

Pointers can be useful for data transmission between a function and its reference point. A function can return only a single value, but through pointers, you can return multiple values from a function via function arguments. In short, pointers provide a convenient way to handle many such other activities.



C++ Pointers Variable:

The variable that is use to hold the memory address of another variable is called a pointer variable or simply a pointer.

The data type of the variable (whose address a pointer is to hold) and the pointer variable must be the same. A pointer variable is declared by placing an asterisk ( * ) after data type or before the variable name in the data type statement.

For example, if a pointer variable “p” is to hold memory address of and integer variable, it is declared as

int* p;

Similarly, if a pointer variable “rep” is to hold memory address of a floating point variable, it is declared as:

float* rep;

the above statement indicate that both “p” and “rep” variable are pointer variable and they can hold memory of integer and floating point variable respectively.

Although the asterisk is written after the data type, it is usually more convenient to place the asterisk before the pointer variable. In this way, a variable with an asterisk is readily recongnized as a pointer variable as shown below:

float *rep;

a pointer variable can also be used to access data of memory location to which it points.

Declaring C++ Pointers:

Since pointers are variables, therefore they must be declared like regular variable. For example: you declare a regular variable as:

int x=5;

The above declaration defines x as an integer variable holding value 5. But think for a moment, where x will be stored in computer memory? What will be the address of x? it would not be sufficient to know your friend’s name only, it would be better if you know his address too, because his address may be helpful for you in time of need. Like this, it you know the address of x. it would be handler to process the value of x.

In C/C++, it is very easy to determine the address f a variable. The ampersand ( &) is used with a variable as a unary operator that evaluates the address of its operand. The unary operator ( &) is also known as address operator. Consider the following example:


now lets assign the address of the x to another variable say ptv as:

ptv=&x;

this new variable is known as pointer variable. Thus ptv represents x’s address, not its value. this is shown in the following figure.

C++ Pointers

The value stored in ptv memory location can be accessed by the expression *ptv. Where * is a unary operator, called ‘value at address’, operator. It returns the value stored at a particular address and operator and operates only on pointer variable. The value at address operator is also called indirection operator. Thus ptv and x both represent the same value. so any variable that is preceded by an asterisk ( * ) is known as a pointer variable. It has the following general format:

Data_type *pointer_variable_name;

For example:

C++ also allows you to declare typed pointer by placing the asterisk after the name of the data type as shown it the following example:

int* ptx;

double* pty;

its important to note that address of the variable will always be whole numbers. Thus pointer would always contain whole numbers.

Look at the following declarations

float *pv;

char *ch;

here pv and ch are declared as a pointer variables. The declaration float *pv does not mean that pv will contain a floating-point value; rather it means that pv will contain the address of a floating-point value. similarly, char *ch means that ch will contain the address of a char value.

further, it is possible to store the address of a variable into another pointer variable. For this we need another pointer, which will contain another pointer’s address for this purpose we use double asterisk with pointers variable. For example **pv ( which indicates a pointer to pointer ).


Example write a C++ program which stores the pointer value into another pointer variable:

Output:

Example write a C++ program that takes input from the user and print the value as well as pointer variable memory location:

Output:



“Void” Type C++ Pointers:

Usually, the type of a variable and the type of pointer variable that holds memory address of the variable must be the same. But the “void” type pointer variable can hold memory address of variable of any data type. A void type pointer is declared by the keyword “void”. The asterisk is used before pointer variable name.

Syntax for declaring void type pointer variable is:

void *p;

the pointer variable “p” can hold the address of variable of any data type.

C++ Pointer and Arrays:

There is a close relationship between pointer and arrays. In advanced programming, array are accessed using pointers.

An array consists of consecutive locations in the computer memory. To access an array, the memory location of the first element of the array is accessed using the pointer variable. The pointer is then incremented to access other elements of the array. The pointer is increased in value according to the size of the elements of the array. When an array is declared the array name points to the starting address of the array. For example, consider the following example:

Int x[5];

Int *p;

The array “x” is of type int and “p” is a pointer variable of  type int.

To store the starting address of array “x” (or the address of first element ). The following statement is used:

P=x;

The address operator (&) is not used when only the array name is used. If an element of the array is used, the ( & ) operator is use. For example, if the memory address of the first element of the array is to be assigned to a pointer, the statement is written as:

P=&x[0];

When an integer value 1 is added to or subtracted from the pointer variable “p” the content of pointer variable  “p” is incremented or decremented by (1 x size of the object or element ), i.e. it is incremented by 1 multiplied with size of the object or element to which the pointer refers.

For example, the memory size of various data types is as shown below:

  • The array of int type has its object or element size of 2 bytes. It is 4 bytes in xenix
  • The array of type float has its object or element size of 4 bytes.
  • The array of type double has its object or element size of 8 bytes.
  • The array of type char has its object or element size of 1 byte.


Example write a C++ program that takes input from the user into an array and find out the maximum value from the array through c++ pointer:

Output:

Passing Pointer as Arguments to Function:

The c++ pointers variable can also be passed to function as arguments. When a pointer variable is passed to a function, the address of the variable is passed to the function. Thus a variable is passed to a function not by its value but by its reference.i.e.

In the above program, the function “temp” has two parameters which are pointer and are of int type. When the function “temp” is called, the addresses of variable “a” and “b” are passed to the function.

In the function, a value 100 is added to both variable “a” and “b” through their pointer. That is, the previous values of variable “a” and “b” are increase by 100. When the control returns to the program, the value of variable “a” is 110 and that of variable “b” is 120.

Let me give you some practical examples.


Example write a C++ program which swaps two value by passing pointer arguments to the function:

Output:

Example write a C++ program which converts Fahrenheit to Celsius using passing pointer arguments to the function:

Output:



Example write a C++ program which converts Kilograms into Grams using passing pointer arguments to the function:

Output:

C++ Pointers and Strings:

A string is a sequence of characters. A string type variable is declared in the same manner as an array type variable is declared. This is because a string is an array of character type variables.

Since a string is like an array, pointer variable can also be used to access it. For example:

char st1[] = “Programming digest”;

char *st2 = “Programming digest”;

in the above statements, two string variable “st1” and “st2” are declared. The variable “st1” is an array of character type. The variable “st2” is a pointer also of character type. These two variable are equivalent. The difference between string variable “st1” and “st2” is that:

  • string variable “st1” represents a pointer constant. Since a string is an array of character type, the st1 is the name of the array. Also the name of the array represents its address which is a constant. Therefore st1 represents a pointer constant.
  • String variable “st2” represents a pointer variable.


Example write a C++ program which counts the length of the string using c++ pointers String :

Output:

Array of  C++ Pointers:

c++ Pointers variable may also be declared as an array.

When an array of strings is declared without using a c++ pointers then each element of the array has a fixed length. If the string entered is shorter than the length of the element, then the remaining space of the element cannot be used. Thus memory space is wasted. To solve this problem, the array of string is declared by using pointer. For example:

char * country [4]={“Pakistan”, “china”, “Sweden”, “Turkey”};

In the above example, the variable “country” is of type “pointer to character”. The four string (names of country) are initialized into it.

Actually the c++ pointers are stored in the array “country”. Each pointer points to the starting address in memory of each string. The array “country” has a fixed length of four and four string of any length can be stored in it. The length of the string that can be stored is not fixed. The storage in memory of the variable country shown below:

C++ Pointers



It the above variable is declared as:

Char country[4][15];

It will fix 4 elements and each element will have a fixes length if 15 characters, including the null character. Each string will occupy 15 bytes in the computer memory even if the string is of a only one character. In this way a large amount of space is wasted. The storage in memory for country[4][15] is shown below.

C++ Pointers

Recommended For You

Leave a Reply

%d bloggers like this: