Tutorial

Arrow operator in C - All you need to know!

Published on August 3, 2022
author

Safa Mulani

Arrow operator in C - All you need to know!

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Hey, folks! In this article, we will be focusing on Arrow operator in C. C language comprises of various operators to deal and manipulate the data records. One such operator is the Arrow operator.

So, let us begin!


Working of Arrow operator in C?

In C, this operator enables the programmer to access the data elements of a Structure or a Union.

This operator(->) is built using a minus(-) operator and a greater than(>) relational operator. Moreover, it helps us access the members of the struct or union that a pointer variable refers to.

Let us now focus on the structure of Arrow operator in C.


Syntax of Arrow operator(->)

Have a look at the below syntax!

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

The operator is used along with a pointer variable. That is, it stores the value at the location(variable) to which the pointer/object points.

Let us now implement this operator through some examples in the upcoming section.


Examples of Arrow operator (->)

In the below example, we have created a Structure ‘Movie_info’. Further, we have assigned a pointer object to the structure and allocated memory to it in a dynamic manner using the C malloc() function.

Arrow operator 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 JournalDev";
     M->ACC="A";
 
     printf("Movie Information:");
     printf("\nName: %s", M->name);
     printf("\nACC: %s", M->ACC);
     return 0;
}

We have accessed the values of the data members using arrow operator(->).

Output:

Movie Information:
Name: Python with JournalDev
ACC: A

Let us now try to access the data members of Union using arrow operator. Arrow operator to access the data members of Union 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;
}

Like Structure, we have created a Union ‘Movie_info’ and have accessd the data values using the arrow operator as shown above.

Output:

Movie Information:
ID: 1
NET VALUE: 125.4

Conclusion

By this, we have come to the end of this topic so feel free to comment below, in case you come across any question.


References

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products


About the authors
Default avatar
Safa Mulani

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
October 26, 2021

as discussed in structure case first we take pointer variable and then access through it. but in case of union we didn’t take pointer variable then also it;s result comes without having an error. could you explain why is it so?

- BHUSHAN

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    September 24, 2020

    Hi int *a is read as "a is pointer to an integer How do we read M->id = 01; ?? M’s id? id member of struct pointed to by M?

    - Praveen Kumar P

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Featured on Community

      Get our biweekly newsletter

      Sign up for Infrastructure as a Newsletter.

      Hollie's Hub for Good

      Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

      Become a contributor

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      Welcome to the developer cloud

      DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

      Learn more
      DigitalOcean Cloud Control Panel