Arrow operator (->) usage in C

arrow operator c
c++ arrow operator function
dot operator vs arrow operator in c
arrow operator overloading in c++
operator name
pointer arrow
c++
structure with arrow operator

I am reading a book called "Teach Yourself C in 21 Days" (I have already learned Java and C# so I am moving at a much faster pace). I was reading the chapter on pointers and the -> (arrow) operator came up without explanation. I think that it is used to call members and functions (like the equivalent of the . (dot) operator, but for pointers instead of members). But I am not entirely sure.

Could I please get an explanation and a code sample?

foo->bar is equivalent to (*foo).bar, i.e. it gets the member called bar from the struct that foo points to.

C++ Member (dot & arrow) Operators, Simply saying: To access members of a structure, use the dot operator. To access members of a structure through a pointer, use the arrow operator. cpp_operators. An Arrow operator in C/C++ allows to access elements in Structures and Unions. It is used with a pointer variable pointing to a structure or union. The arrow operator is formed by using a minus sign, followed by the geater than symbol as shown below.

Yes, that's it.

It's just the dot version when you want to access elements of a struct/class that is a pointer instead of a reference.

struct foo
{
  int x;
  float y;
};

struct foo var;
struct foo* pvar;
pvar = malloc(sizeof(pvar));

var.x = 5;
(&var)->y = 14.3;
pvar->y = 22.4;
(*pvar).x = 6;

That's it!

Arrow operator (->) usage in C, Arrow operator (->) usage in C · c pointers syntax. I am reading a book called "​Teach Yourself C in 21 Days" (I have already learned Java  The dot and arrow operator are both used in C++ to access the members of a class. They are just used in different scenarios. In C++, types declared as a class, struct, or union are considered "of class type". So the following refers to both of them.

a->b is just short for (*a).b in every way (same for functions: a->b() is short for (*a).b()).

C Programming arrow operator, Arrow operator (->). Arrow operator is used for accessing members of structure using pointer variable, below is the syntax of arrow operator in c  We can use Arrow Operator (->) to access class members instead of using combination of two operators Asterisk (*) and Dot (.) operator, Arrow operator in also known as “Class Member Access Operator” in C++ programming language.

I'd just add to the answers the "why?".

. is standard member access operator that has a higher precedence than * pointer operator.

When you are trying to access a struct's internals and you wrote it as *foo.bar then the compiler would think to want a 'bar' element of 'foo' (which is an address in memory) and obviously that mere address does not have any members.

Thus you need to ask the compiler to first dereference whith (*foo) and then access the member element: (*foo).bar, which is a bit clumsy to write so the good folks have come up with a shorthand version: foo->bar which is sort of member access by pointer operator.

Why does the arrow (->) operator in C exist?, is formed by using a minus sign, followed by the geater than symbol as shown below. Arrow operator is used for accessing members of structure using pointer variable, below is the syntax of arrow operator in c programming – Syntax of arrow operator. struct student { char name [20], int roll; }* ptr; Whenever we declare structure variable then member can be accessed using the dot operator. But when pointer to a structure is

foo->bar is only shorthand for (*foo).bar. That's all there is to it.

What is operation of the dot(.) symbol in C?, “->” This sign is known as arrow operator in C/C++. This sign (->) provides to access object in structures and unions. These signs mostly used in pointer variables  It's worth noting that if the dereference operator had been made postfix, as in Pascal, the -> operator would not have been needed at all, as it would have been equivalent to the much more legible foo*.bar. The whole mess of typedef-ing functions with all the extra parentheses would have been avoided as well. – user207421 Jan 26 '15 at 6:04.

What does '->' mean in C/C++ programming?, Operator. We can use Arrow Operator (->) to access class members instead of using combination of two operators Asterisk (*) and Dot (.) operator,  Although the arrow in an arrow function is not an operator, arrow functions have special parsing rules that interact differently with operator precedence compared to regular functions.

Arrow Operator as Class Member Access Operator in C++ , dot and The (->) arrow operator. Jul 25, 2014 at 11:08am. Jul 25, 2014 at 6:08pm UTC. Jacobhaha (153). Hello, can someone explain the difference between  The 'arrow' operator is syntactic sugar. bar->member is the same as (*bar).member. One reason for the difference is maintainability. With the arrow operator distinct from the dot operator, it becomes much easier to keep track of which variables are pointers and which are not.

The (.) dot and The (->) arrow operator. - C++ Forum, Using arrow operator (->). The above method of accessing members of the structure using pointers is slightly confusing and less readable, that's why C provides  Fat arrow notations are used for anonymous functions i.e for function expressions. They are also called lambda functions in other languages. Using fat arrow (=>) we drop the need to use the 'function' keyword. Parameters are passed in the angular brackets <>, and the function expression is enclosed within the curly brackets {}.

Comments
  • Get a better book. norvig.com/21-days.html
  • qrdl is correct -- the "Learn X in Y days" books are generally garbage. In addition to K&R, I would also recommend Prata's "C Primer Plus", which goes into more depth than K&R.
  • @Steve That question deals with C++. Calling it a caused some confusion for me when I started reading about operator overloading in that other answer, which is not relevant in C.
  • @Belton The hard way series are bad, the guy says stuff that wasn't even relevant when he wrote the book and he doesn't care about good practices.
  • The Peter Norvig link to "Teach Yourself Programming in Ten Years" is great, one of my favorites. Here's the comic version which explains how to do this in 21 days, which I unfortunately remembered as an XKCD but I was wrong: abstrusegoose.com/249
  • It's worth noting that if the dereference operator had been made postfix, as in Pascal, the -> operator would not have been needed at all, as it would have been equivalent to the much more legible foo*.bar. The whole mess of typedef-ing functions with all the extra parentheses would have been avoided as well.
  • So would foo*.bar and (*foo).bar both be equivalent to foo->bar? What about Foo myFoo = *foo; myFoo.bar?
  • No, he is just saying IF the creators of C would have made the dereference operator as POSTfix operator instead of PREfix then it would have been more easy. But it IS a prefix operator in C.
  • @user207421 Coulfd you please give a short description or link to the "typedef-ing functions with all the extra parentheses" that you mention? Thanks.
  • @user207421 nah, it would cause more parents.. so far, there is priority of () and [] to the right above * to the left. if they all on one side, you'll have put more parents. Same in expressions, because of conflict with multiplication operator. Pascal ^ could be an option but it was reserved for bit operation, still more parents.
  • Since pvar is uninitialised, how would you initialise it if you wanted pvar to point to a new struct, that is not pvar = &var?
  • The question was specifically about C, which does not have classes or reference variables.
  • hmm shouldn't you do a malloc before writing to pvar struct foo* pvar; ?? pvar->y write to unallocated space!