C Programming

Arrow -> Operator in C with Examples

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.

$ touch main.c

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:

$ nano main.c

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.

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.

$ gcc main.c

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.

$ ./a.out

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.

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

$ gcc main.c

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”.

$ ./a.out

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.

$ nano main.c

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.

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.

$ gcc main.c

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.

$ ./a.out

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.

$ nano main.c

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.

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

$ gcc main.c

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.

$ ./a.out

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

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.