Array of pointers C++

Array contains the list of elements according to the size we provide at the time of array creation. Whereas, in C++, pointers are the variables that have the addresses of other variables. These pointers have the address of a single variable and can store the address of the array’s cells. Both arrays and pointers are very closely related to each other as in C++, array’s name is considered as a pointer because it consists of the address of element. Hence array contains the elements, and pointers contain the address of variables. So the pointer array or ‘array of pointer’ depicts an array with the address of the elements present in the array. This topic will contain the phenomenon of the array of pointers in C++.

Syntax

In C++, if we have to declare an array of pointers, we create an array that contains the address of the elements present inside it that points to some address values.

# type *pointer_name [array_size];

According to the syntax, if you are willing to create a pointer array, we define the type of array pointer. After that, the name of the pointer array is declared. As you can see from the syntax that “*” is used with the name of a pointer in C++. After naming the array, the size of the array is declared that shows how many elements will be present in the array.

# Int *newp[5];

Working of pointers array in C++

The pointer value points to the address of the values present inside the array. If you are willing to access the values, we can access each value by using their address because it only points to that specific address. The use of pointers makes the functionality operations more efficient and also affects the performance level. Now we will see that how to declare a pointer array.

As we have used a sample declaration of pointer array above.

# Int *newp[5];

In this above line, we have declared an array of pointers having 5 elements. This array will contain the address of the values in it. The address is the location of the element where the array is stored inside the memory. This memory address always turns the point to the element that is stored at that location.

Creation of array of pointers in C++

There are some steps to create an array of pointers in C++

First, we create an array having elements. Suppose we have 5 elements.

# Int newarray [5] = {1,2,3,4,5};

After that, we create a pointer array that stores the address of elements of the array.

# Int "newp[5];

If you want to get the address of elements in the array, use the ‘&’ operator, this will give us the address of the values in the memory.

# Newp[1]= &newp[1];

After that, the address of elements is stored into the arrays of pointers by using the loop.

Now we can access the elements in the array with the pointers; it will provide the same value.  Now we will use some elementary examples here that will help you in the understanding concept.

Example 1

In this example, we have simply displayed the values inside the array. But this time, it is not done by displaying the values through the inside number but by using pointers. So first step in the main program, we dynamically create the array of size 5.

# Int*p = new int[5];

After that, as we have described before in the topic’s part “creation of an array of pointers in C++”, the array is initialized with the numbers. We will use for loop to enter the values in the respective indexes. This is done through the pointers. ’10’ is a constant here that is used to multiply the value with the upcoming one; this is a smart approach to assigning the values.

# 2[p]

Now the value of p is 1, so after multiplying, it will be 2, at the point.

For instance, when the loop iterates for the first time, the value of “I” will be ‘0’, so when in the parenthesis, it will be added with 1, it will become 1, and after multiplying with the constant, the result will be equals to the constant itself.  For the second index, in the next iteration, when the value of I is ‘1’, after addition with 1, it will be 2, so when it is multiplied by 10, it will become 20. And then so on in each iteration till the value to be entered is 50. In the case of displaying the values through pointers, we have used different techniques; these will surely be beneficial for you in understanding perspective. First output providing statement contains:

# *p

As we know that this ‘*’ symbol displays the address, one thing should be kept in mind: when we use a pointer to display the value without using the index, it automatically assigns the first value by default, the result will be 10. The next is:

# *p + 1

It will simply add the default value with one, so the answer is 11. Moving towards the next value,

# *(p + 1)

Now this time, we will talk about the index but not the address as “*” is not with p. So it denotes to ‘0’, this 0 will be added with 1, and forms *(1), so at 1 position, it is 20, so it will be displayed.

Similarly, other results will be displayed. In the end, the pointer is terminated as we have also taken the result of that incremented value.

For the resultant value, go to the terminal of Linux and use the g++ compiler to compile and execute the code.

$ g++ -o array array.c
$./array

Example 2

This example relates to the displaying of addresses using arrays and pointers to display the difference between them. For this purpose, in the main program, we declare an array having a float data type. The float pointer variable is declared.

# *ptr;

Now with the help of this pointer, we will be able to display the address. But first, let us display the address of the elements by using an array. This is done through a FOR loop. This is the easy and generic way of displaying the array’s content through the index number.

# Ptr = arr;

Using the pointer notations, we will display the address through the pointers. Again a FOR loop is used to display the address through the pointer.

Again use the g++ compiler to compile and then execute the code in the Linux terminal to display the resultant values.

As you execute the code, you will see that the answer for both the methods is the same; either through an array or through the pointers, the same result is obtained.

Conclusion

An array of pointers is used in C++ in the Ubuntu Linux operating system to elaborate on fetching data through the address and arrays. This article was all about the array of pointers in C++. We have elaborated on the syntax and a few examples related to pointers. These examples can be implemented on any compiler as per the user’s choice.



from https://ift.tt/3dpFaGk

Post a Comment

0 Comments