I am currently learning C by reading a good beginner's 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?
相关问题
- Multiple sockets for clients to connect to
- Do the Java Integer and Double objects have unnece
- What does an “empty line with semicolon” mean in C
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
foo->bar
is only shorthand for(*foo).bar
. That's all there is to it.foo->bar
is equivalent to(*foo).bar
, i.e. it gets the member calledbar
from the struct thatfoo
points to.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.
That's it!
The
->
operator makes the code more readable than the*
operator in some situations.Such as: (quoted from the EDK II project)
The
_EFI_BLOCK_IO_PROTOCOL
struct contains 4 function pointer members.Suppose you have a variable
struct _EFI_BLOCK_IO_PROTOCOL * pStruct
, and you want to use the good old*
operator to call it's member function pointer. You will end up with code like this:(*pStruct).ReadBlocks(...arguments...)
But with the
->
operator, you can write like this:pStruct->ReadBlocks(...arguments...)
.Which looks better?