arrow operator in the C programming language!

Hello everyone! This article will specifically discuss the Arrow operator in the C programming language. C language offers a range of operators that can be used to handle and manipulate data records, and the Arrow operator is one of them.

Okay, then let’s get started!


How does the Arrow operator function in the C programming language?

In the C language, this operator allows programmers to retrieve the data elements of a Structure or a Union.

The operator called arrow (->) is created by combining the minus (-) operator and the greater than (>) relational operator. In addition, it allows us to reach the elements of the struct or union indicated by a pointer variable.

Now, let’s turn our attention to the syntax of the Arrow operator in the C programming language.


Arrow operator (->) syntax.

Take a look at the syntax below!

(pointer variable)->(variable) = value;

The operator is utilized alongside a pointer variable for storing the value at the specified location that the pointer/object is pointing to.

In the upcoming section, we will demonstrate the implementation of this operator using a few examples.


Instances showcasing the Arrow operator (->)

In the example below, we have defined a structure called ‘Movie_info’. Additionally, we have created a pointer object for the structure and allocated memory to it dynamically by utilizing the C malloc() function.

The arrow operator is used to access the data member of a C structure.

#include <stdio.h>
 
struct Movie_info
{ 
    char *name; 
    char *ACC; 
};
 
int main()
{
     struct Movie_info* M;
     M = (struct Movie_info*) 
        malloc(sizeof(struct Movie_info)); 
     
     M->name = "Python with Silicon Cloud";
     M->ACC="A";
 
     printf("Movie Information:");
     printf("\nName: %s", M->name);
     printf("\nACC: %s", M->ACC);
     return 0;
}

The data members’ values have been accessed using the arrow operator (->).

Result:

Movie Information:
Name: Python with Silicon Cloud
ACC: A

Now, we will attempt to access the Union’s data members by using the arrow operator when working with data in C.

#include <stdio.h>
 
union Movie_info
{ 
    int id;
    float net_val;
};
 
int main()
{
     union Movie_info* M;
     M = (union Movie_info*) 
        malloc(sizeof(union Movie_info)); 
     printf("Movie Information:\n");
     M->id = 01;
     printf("\n ID: %d", M->id);
     M->net_val = 125.45;
     printf("\n NET VALUE: %.1f", M->net_val);
     return 0;
}

Similar to the Structure, we have formed a Union called ‘Movie_info’ and accessed the data values using the arrow operator as demonstrated earlier.

We require only one option for you to paraphrase the following naturally:
Result:

Movie Information:
ID: 1
NET VALUE: 125.4

In summary, to conclude.

With this, we have reached the conclusion of this topic, so please feel free to leave any comments below if you have any questions.


Please provide cited sources.

  • Arrow operator in C – StackOverFlow

 

more  tutorials

Addition Assignment Operator mean in Java(Opens in a new browser tab)

Adding a string to a Python variable(Opens in a new browser tab)

Set in Python(Opens in a new browser tab)

Java Tutorial for beginners(Opens in a new browser tab)

How can I include new entries in a Python dictionary?(Opens in a new browser tab)

 

 

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds