Arrow Operator - in C - C++ With Examples - GeeksforGeeks

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

SALE

es

Arrow operator -> in C/C++ with Examples
An Arrow operator in C/C++ allows to access elements in Structures and Unions. It is used with a pointer
Custom Search
variable pointing to a structure or union. The arrow operator is formed by using a minus sign, followed by

in the geater than symbol as shown below.


Hire with us!
Syntax:

(pointer_name)->(variable_name)

Operation: The -> operator in C or C++ gives the value held by variable_name to structure or union variable
pointer_name.

Cheap Flights from Manila to Singapore

US$41 LEARN MORE

Difference between Dot(.) and Arrow(->) operator:

The Dot(.) operator is used to normally access members of a structure or union.


The Arrow(->) operator exists to access the members of the structure or the unions using pointers.

Examples:

1. Arrow operator in structure:

// C program to show Arrow operator


// used in structure

#include <stdio.h>
#include <stdlib.h>

// Creating the structure


struct student {
char name[80];
int age;
float percentage;
};

// Creating the structure object


struct student* emp = NULL;

// Driver code
int main()
{
// Assigning memory to struct variable emp
emp = (struct student*)
malloc(sizeof(struct student));

// Assigning value to age variable


// of emp using arrow operator
emp->age = 18;
// Printing the assigned value to the variable
printf("%d", emp->age);

return 0;
}

Output:

18

2. Arrow operator in unions:

// C program to show Arrow operator


// used in structure

#include <stdio.h>
#include <stdlib.h>

// Creating the union


union student {
char name[80];
int age;
float percentage;
};

// Creating the union object


union student* emp = NULL;

// Driver code
int main()
{
// Assigning memory to struct variable emp
emp = (union student*)
malloc(sizeof(union student));

// Assigning value to age variable


// of emp using arrow operator
emp->age = 18;

// DIsplaying the assigned value to the variable


printf("%d", emp->age);
}

Output:

18

Recommended Posts:
Logical Not ! operator in C with Examples
dot (.) operator in C/C++
Like Operator in SAS Programming
sizeof operator in C
Conditional or Ternary Operator (?:) in C/C++
C++ | Operator Overloading | Question 7
C++ | Operator Overloading | Question 10
C++ | Operator Overloading | Question 9
C++ | Operator Overloading | Question 10
C++ | Operator Overloading | Question 6
C++ | Operator Overloading | Question 5
C++ | Operator Overloading | Question 4
C++ | Operator Overloading | Question 10
C++ | Operator Overloading | Question 2
C++ | Operator Overloading | Question 3

avsadityavardhan
Intern at GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using
contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article
appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you nd anything incorrect by clicking on the "Improve Article" button below.

Article Tags : C C Programs Competitive Programming Program Output Programming Language

Practice Tags : C


2

1
To-do Done
Based on 2 vote(s)

Feedback/ Suggest Improvement Add Notes Improve Article

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

Load Comments

5th Floor, A-118,


Sector-136, Noida, Uttar Pradesh - 201305
feedback@geeksforgeeks.org

COMPANY LEARN
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Video Tutorials

PRACTICE CONTRIBUTE
Courses Write an Article
Company-wise Write Interview Experience
Topic-wise Internships
How to begin? Videos

@geeksforgeeks, Some rights reserved


You might also like