C++ Pointer Arithmetic

We know that the pointers are memory addresses of variables and are numeric in general. That’s why we can do some arithmetic calculations on the pointers as well. These calculations can be done the same as we do on numerical values in mathematics or programming. Therefore, we will be discussing some arithmetic operations on the pointers within our tutorial guide using C++. We have been using the Ubuntu 20.04 system making sure to have the G++ compiler configured in it. Let’s begin with the implementation of pointer arithmetic in the Ubuntu 20.04 system terminal shell with the help of a shortcut Ctrl+Alt+T used on its desktop.

Example 1:

Begin the first example of C++ code to use arithmetic operators “+” on pointers by file creation. You can create a file in many ways but the simplest one is the use of “touch” instruction. Thus, we have tried the touch keyword along with the title of a file to be formed on the console and pressed Enter. The file is created in the Ubuntu 20.04 home folder.

Now, to open this newly created file, you can use any of the built-in editors that come with Ubuntu 20.04 i.e. vim, text editor, or GNU nano editor. We suggest you use the GNU Nano editor as we have been using it in the shell as demonstrated from the image below.

This file “pointer.cc” is unbolted in the editor so far. We have included the input-output stream header file at the first line and the standard namespace has been used at the second line. We have initialized an integer variable “v” with a value of 8. At the next consecutive line, we have initialized an integer type pointer “p”. This pointer is the address of a variable “v” as it is bound by the variable “v” using the “&” sign. This means we can change the address of a mutable at any time. The standard cout statements have been used one after another. The first one is displaying the original address of variable “v” saved as the pointer “p”.

On the next line, the cout statement has been incrementing the address of the pointer by 1 and displaying it. The next two lines have been using the pointer and incrementing its value by 2 and 3. The incremented values have been displayed with the cout statement. The code ends here. Let’s save the code first before the execution. Use Ctrl+S for this purpose. You have to quit the GNU Nano editor to come back towards the terminal. Make use of the “Ctrl+X” shortcut key for this purpose. This was the simplest C++ code to increment the pointer using the + operator.

After coming back to the shell, we have to make our code error-free. The C++ compiler is used for this perseverance. The compiler keyword “g++” has been used along with the name of a file to be compiled i.e. “pointer.cc”. The compilation is successful as you can see that it returns nothing.  Let’s execute our error-free code using the ”./a.out” standard command. We have got the 4 different addresses for variable “v”.

The first one is the original address “p” of variable “v”. The second one is incremented by 1, the third one is incremented by value 2 and the last one is incremented by 3. Every time, we perform the increment, the last two characters of an address tend to change as shown beneath.

Example 2:

Let’s make another example to use the decrement operator on a pointer. So, we have been consuming the same old file “pointer.cc”. The namespace and input-output header are used the same as we did before. Another constant integer variable “s” is initialized with a constant value of 5. Within the main() method, we have been using an integer type array named “v” of size “s” with 5 elements in it. The integer pointer “p” has been declared. The pointer has been bound with the integer array “v” using the “&” sign.

The size will be started from the s-1 address. The “for” loop has been initialized which is started from size 5 and works in the decreasing order by decrementing by 1 each time. Every time the “for” loop works, it displays the memory address of the index number iterated by the loop and the value at a particular index as well using the standard cout statement. The “p” demonstrates the index address, while the *P represents the value at that particular index. In each iteration, the pointer has been decremented by 1. The loop ends here and the main function as well.

Compile the code first with the g++ compiler of the C++ language. It runs successfully with no errors. The execution is performed by the “./a.out” command. We have got the output as shown below. You can see, we have got each memory address for a particular index i.e. 5,4,3,2,1 in the decreasing order of indexes. On the other hand, we have also got the values at each particular index every time the loop iterates in the decreasing order until the last value.

Example 3:

Let’s have a new instance of pointers. Within this example, we will be comparing pointer addresses as well as the values they contain. Thus, the document pointer.cc is now launched in the GNU Nano editor. The main() function has been initialized after the standard namespace and “io” stream header in the code file. It contains two string type variables s1 and s2 with completely different string values i.e. “Aqsa” and “Yasin”.

After this, we have initialized two string type pointer variables p1 and p2 bounded by both variables s1 and s2 using the “&” character after the “=” sign. This means that the pointer p1 is the address of variable s1 and p2 is the address of variable s2.

The first standard cout clause is used to show the comparison result of both pointers i.e. addresses of both string variables. If the addresses are the same, it will display 1 as true otherwise 0 as false on the shell. The second standard cout clause is used to display the comparison result of values stored in the specific pointer address. If the values are the same, it will return 1, otherwise 0. The comparison program ends here.

Compile your C++ code first and execute it. We have got 0 as a result of both comparisons i.e. false. This means that both pointer addresses and values at those addresses are not the same.

Let’s change the code a little. Open the same file, and update the string values. The same string variables s1 and s2 have been initialized with the same values i.e. Aqsa. The rest of the code is used unchanged as we did before. Save your code to get the updated result.

We have got 0 as the comparison result of pointer addresses as both variables contain different memory addresses and 1 as the result of value comparison i.e. same values of both strings.

Conclusion:

We have discussed the arithmetic operations performed on the pointers. We have used the increment and decrement arithmetic operators on pointers. We have also discussed the bonus examples to illustrate the working of the comparison operator on two different pointers.



from https://ift.tt/3rgfhRp

Post a Comment

0 Comments