Tutorials  /  Linux Basics

Maximize Your Understanding of the Arrow Operator in C

Ccentron Redaktion · October 2024 ·3 min read ·Linux Basics, Tutorial

In this post, we focus on the arrow operator in C. The C programming language offers a variety of operators to work with and manipulate data sets. One of these operators is the arrow operator.

Let’s dive right in!

How Does the Arrow Operator Work in C?

In C, the arrow operator allows access to the data of a structure or union. This operator (->) is made up of a minus sign (-) and a greater-than sign (>). It enables referencing the elements of a structure or union pointed to by a pointer variable.

Let’s take a closer look at the syntax of this operator.

Syntax of the Arrow Operator (->)

Here’s the general syntax:

Code
(pointer_variable)->(variable) = value;

The arrow operator is used with a pointer variable. This means the operator accesses the address (variable) pointed to by the pointer and stores a value there.

Let’s look at some practical examples.

VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Examples of the Arrow Operator (->)

In the following example, we’ve created a structure called Movie_info. We assign a pointer to this structure and dynamically allocate memory using the C function malloc().

Arrow Operator for Data Access in a C Structure:

Code
#include <stdio.h>
#include <stdlib.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 = "C with JournalDev";
     M->ACC = "A";
 
     printf("Movie Information:");
     printf("\nName: %s", M->name);
     printf("\nACC: %s", M->ACC);
     return 0;
}

In this example, we access the values of the data members using the arrow operator (->).

Output:

Code
Movie Information:
Name: C with JournalDev
ACC: A

Now, let’s see how to access the data members of a union using the arrow operator.

Arrow Operator for Accessing Data Members of a Union in C:

Code
#include <stdio.h>
#include <stdlib.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 created a union Movie_info and accessed its values using the arrow operator.

Output:

Code
Movie Information:
ID: 1
NET VALUE: 125.4

Conclusion

We’ve now reached the end of this topic. The arrow operator in C is a powerful tool for accessing the data of structures and unions referenced by pointers. If you have any questions, feel free to leave a comment!

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren