Procedure
In this article, we will try to learn what an arrow operator is, what syntax is used for this operator, and why we use this operator. Since the arrow operator points to the memory address of any variable or the element of the structure/union that we have defined in our code, so we will also discuss the implementation of the pointer variable with the arrow operator by implementing different examples in Visual Studio (C).
Syntax
The syntax of the arrow operator is as simple as many other operators in different programming languages. We use this operator in between a pointer variable and the element we need to access in the following way:
Example # 01
As we have already discussed in the introduction of this article, we use the arrow operator to either access the members or elements in the structure or the union. Here in the first example, we would like to use the arrow operator for the pointer variable to get access to the members of the structure. To implement this example, we first have to define a structure in the code, but before that, we will need to import the given library”:
We may call this library to be the header file for the C in the visual studio. This header file contains all the information to implement the code in the C. This library lets us include the information related to the printing of the output, scanning of the output, and getting the input from the user as well.
Now coming back to the example, we will construct a structure using Struct with a name; let’s assume a “restaurant”. Struct is used in C to define the declaration of the data type of list containing a group of variables that a pointer can access. We now need to define the elements of the structure so, for a restaurant, we will define two elements, e.g., name and number of rooms in the restaurant. To define the data type of “name” as an element of the structure, we will use the character “char name” with the array size of 20, and for the “# of rooms in the restaurant”, we will define the data type as an integer “int”.
Till now, we have constructed the structure that we will access later on with the help of the arrow operator. In the main, we will assign the values to the elements of the structure ( e.g., struct restaurant { Tulip, 34} ) and will define a pointer variable as *ptr. Then we will make the pointer ptr to point to the structure restaurant by using the operator “&” and “name of structure” as mentioned below:
Now the “ptr” is pointing towards the structure “restaurant”. If we now want to know the information regarding the elements of structure that we have already stored, we will do this by using the ptr with the element name and then display it using “printf,”e.g. “printf ( “name % r \n”, ptr-> name)” and then use the command getch () to let the “ptr->name assign the values of character array “tulip” to element name and “ptr->room” (the integer value “34”) to the element # of rooms in the structure restaurant.
struct restaurant
{
char name[20];
int rooms;
};
void main ()
{
struct restaurant restaurant = {“tulip", 34};
struct restaurant* ptr;
ptr = &restaurant;
printf ("name: %restaurant\n", ptr->name);
printf ("rooms: %d\n", ptr->rooms);
getch ();
}
In the output, we have accessed both elements of the structure using the arrow operator.
Example # 02
The above-mentioned example has implemented the arrow operator using a pointer variable to get access to the elements of the structure. In the second example, we will use the arrow operator as the pointer to get access to the members of the union. Union is known as data type declaration in C since it can store the multiple variables of different data types into a single memory location having the same memory address, unlike the structure where every variable has a different memory address.
To create the union, let’s first include all the information of printf and scanf in the project by including the header “# including <stdio.h>”. Then move forward to creating a union by using the data type declaration method “union” and then name the union. Here we have chosen the “country” to be the union name. Now we will define the elements of union, e.g., citizen_id, citizen_net_income, etc. We will then declare the citizen_id data type to be an integer and the citizen_net_income data type to float.
In the main, we will define a pointer “*ptr” of union and allocate it the memory size (dynamic) using the “malloc ()” method. After following the above steps successfully, we will use the ptr with arrow operator and assign the values to the elements of the union as “ptr-> citizen_id =01” and return 0 to exit the code.
In the example, we have tried to access the element of the union “citizen_id” and assign it the value “01” using the arrow operator.
Conclusion
The article covers the “Arrow operator” in C. With the help of this guide, we may learn why we use the arrow operator with an explanation of the syntax of the operator. We have used the arrow operator along with the pointer variable to access the members of the union or structure. This guide shows the demonstration of the arrow operator with the help of two examples. We are in good hopes that you will find the best use of this article to enhance your knowledge of using the arrow operator in your codes.