To understand the working of the Arrow operator, we need to understand what pointer is first. In C , pointer exists as a mutable, which always refers to the location of some other mutable of almost any data kind, such as an int, char, or float, since we’ve already taught. A pointer to structures could be used likewise, with a pointer mutable pointing to the location of such a structure mutable. Throughout C/C , the Arrow operator may be used to access items in Unions plus Structures. It can be used in conjunction with such a pointer variable that points to a structure and union. A negative sign precedes the greater than mark to construct the arrow operator. Throughout C or C , the -> operator assigns the content of the mutable name to the structural or union mutable pointer name.

Syntax

(pointer_name) -> (variable_name)

Example 01

To elaborate on the working of the arrow operator, we will be discussing some examples. Open your Ubuntu 20.04 system and log in from it. Open the shell by Ctrl Alt T or via the activity search bar. Now make sure you have a C compiler configured on the Ubuntu system. Make a new file named “main.c” using a touch command in the terminal.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Arrow-Operator-C-Examples-01.png" data-lazy- height="29" src="data:image/svg xml,” width=”489″>

The file will be created in the home directory. You can open the file by double-clicking on it or via terminal using the “GNU” nano editor command as follows:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Arrow-Operator-C-Examples-02.png" data-lazy- height="31" src="data:image/svg xml,” width=”491″>

The appended example illustrates how to construct with a pointer. You understand that perhaps the array’s title (ptr bd->name) is a fixed pointer that refers towards the array’s 0th item. Because you can’t use the equal sign (=) to allocate a new string to it, the strcpy() method is utilized. Because the primacy of the arrowhead operator (->) is larger than that of the precursor reduction operator (–) in this phrase, the result of the words is reduced by one after the -> sign has been used. We’ve specified a structure with 4 data members: name, type, age, and color for the category bird. A structure bird mutable named bd is defined and initialized. A structure bird reference variable, bd, has been described. The & expression is used to allocate the location of bd to ptr bd. The printf() instructions display the bird’s information. Since you can’t provide a string value toward ptr_bd->name through using equal sign, we have utilized the strcpy() method to create an entirely new name. By using the postfix increment operator, the amount of ptr_bd-> age is increased by one. Keep in mind that perhaps the postfix and -> operators have almost the same priority and are associated from left to right. However, when postfix is being used in the equation, the amount of ptr_bd-> age is utilized initially, and its amount is then increased by 1. Save the code and quit the file by Ctrl S followed by Ctrl X.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Arrow-Operator-C-Examples-03.png" data-lazy- height="595" src="data:image/svg xml,” width=”706″>

The gcc compiler has done a compilation of the above C code in the shell via the below instruction followed by the name of a file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Arrow-Operator-C-Examples-04.png" data-lazy- height="29" src="data:image/svg xml,” width=”504″>

Execution of the file has been done after its compilation using the below query in the Ubuntu 20.04 shell. The output is showing the Bird’s name, type, age, and color. Then the name of a bird has been changed, and Its age has been incremented by 1.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Arrow-Operator-C-Examples-05.png" data-lazy- height="177" src="data:image/svg xml,” width=”437″>

Example 02

For a better understanding of the Arrow operator, let’s see another instance. We have updated the code as presented in the image. First, we have created a structure “person” with some data members, e.g., name, age, and percent. Then we have created the object of a structure and gives it a NULL value. The primary function has been used to drive the C code. In this main method, the structure variable “emp” has been assigned with a memory location using the “malloc()” method. The structure variable’ “emp” value has been given to variable “age” via arrow operator.  Then it will print the value that has been assigned to a variable “age,” and the main method ends. Save the code and turn it back to the terminal using Ctrl S and Ctrl X simultaneously.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Arrow-Operator-C-Examples-06.png" data-lazy- height="481" src="data:image/svg xml,” width=”691″>

Compilation of the overhead C code has been done through gcc compiler as follows:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Arrow-Operator-C-Examples-07.png" data-lazy- height="29" src="data:image/svg xml,” width=”504″>

Execution of the file can be done using the same a.out command as below. The output shows the value of a variable age as “45”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Arrow-Operator-C-Examples-08.png" data-lazy- height="50" src="data:image/svg xml,” width=”427″>

Example 03

In this example, we will see the illustration of using the arrow operator on the union structure. Hence, open the main.c file again via terminal.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Arrow-Operator-C-Examples-09.png" data-lazy- height="31" src="data:image/svg xml,” width=”525″>

We have updated the code and included two libraries in our code. First, we have created a Union structure “person” with the same data members, e.g., name, age, and percent. The object of a union structure has been created. The main method has been started. The execution of a code will always be started from the main method. In the main function, the union mutable “emp” has been allocated with a memory location by utilizing the “malloc()” function. The method “sizeof()” has been castoff to get the size of a union “person.” The union mutable’s “emp” value has been allotted to mutable “age” utilizing the arrow operator.  After that,  we will be consuming a printf statement to print the value that has been allocated to a mutable “age,” and the main method finishes. Save the code and go it back to the terminal employing Ctrl S and Ctrl X one after the other.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Arrow-Operator-C-Examples-10.png" data-lazy- height="458" src="data:image/svg xml,” width=”672″>

A gcc compiler again completes the compilation of the above-shown code. That’s why we have to use the below-stated instruction in your Ubuntu 20.04 system to compile it.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Arrow-Operator-C-Examples-11.png" data-lazy- height="31" src="data:image/svg xml,” width=”493″>

Let’s execute the file main.c once again using the same query for execution in the terminal of the Ubuntu 20.04 system. The output is as same as the output of the above example for the structure.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Arrow-Operator-C-Examples-12.png" data-lazy- height="58" src="data:image/svg xml,” width=”479″>

Example 04

Let’s have a last example to see the working of the arrow operator on the structure. Open your file once again in the “Nano” editor.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Arrow-Operator-C-Examples-13.png" data-lazy- height="29" src="data:image/svg xml,” width=”502″>

We have created a structure “Drama” with two data members: name and episodes, aka epi. In the main method, we have made an object of structure “Drama.” Then variable “d” has been given a memory space via the method “malloc” and using the structure object. The value of the variable “d’ has then assigned using the arrow operator pointer to the variable “name” and “epi” one after another. Both the variables are printed out in the shell via printf statement.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Arrow-Operator-C-Examples-14.png" data-lazy- height="529" src="data:image/svg xml,” width=”750″>

Pile up your above C code via the stated-below query:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Arrow-Operator-C-Examples-15.png" data-lazy- height="27" src="data:image/svg xml,” width=”495″>

Execution of the code by a.out command has shown the below results. It tells us the Drama name and its total episodes that have been pointing by an arrow operator.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Arrow-Operator-C-Examples-16.png" data-lazy- height="85" src="data:image/svg xml,” width=”470″>

Conclusion

Throughout this article, we have efficiently elaborated the examples of arrow pointer. We have also discussed the usage of the arrow operator within structure and union. Hope it will be helpful for you while using the concept of arrow operators.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Author-Image-150×150.jpg60eb762defd82.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.