tags:

views:

252

answers:

10

I have looked at some resources to tell me how -> and . are different, but they seem to do the same thing. Does -> act like a dot operator on a struct?

+2  A: 

you're using a dot when accessing object's members, and the arrow -> when accessing members through the pointer to an object

migajek
+2  A: 

-> is to a struct pointer what . is to a struct.

struct Data data;
data.content = 1;

struct Data* pData = &data;
pData->content = 2;
Vincent Robert
+16  A: 

. is used when you have a struct, and -> is used when you have a pointer to a struct. The arrow is a short form for dereferencing the pointer and then using .: p->field is the same as (*p).field.

Graeme Perrow
Worth mentioning that this is really nice when you have pointers to **other** structs in a struct. So you have `a->b->c->d` instead of `(*((*((*a).b)).c)).d`.
detly
+2  A: 

Ah, I just came across the same question, when looking at locale settings. One is for accessing the attributes through the pointer and one is for the dereferenced struct:

#include <locale.h>

int main (void) {

    struct lconv *locale_ptr; 
    locale_ptr = localeconv();
    printf("Currency symbol: %s\n", (*locale_ptr).currency_symbol);
}

is equivalent to:

int main (void) {

    struct lconv *locale_ptr; 
    locale_ptr = localeconv();
    printf("Currency symbol: %s\n", locale_ptr->currency_symbol);
}
chryss
+6  A: 

They are almost the same thing. The only difference is that "->" takes a pointer to a struct on the left side while "." takes a struct; "->" deferences (i.e. follows) the pointer before accessing the struct member. So,

struct foo bar;
bar.x = 0;

is the same as:

struct foo bar;
struct foo *diddly = &bar;
diddly->x = 0;
fool4jesus
+1  A: 

Most simply you use . when operating on a Struct itself and -> when operating on a pointer to a struct.

To show in code:

  struct s myStruct;
  myStruct.num = 5;

Is valid, but:

  struct s myStruct;
  myStruct->num = 5;

Is invalid as myStruct is not a pointer.

  struct s *myStruct;
  myStruct->num = 5;

Would be valid.

The -> operator is actually a shorthand for (*myStruct).num;

sir.jamesgreen
+1  A: 

The C language, unlike many other languages allows variables to have objects (here structs) as values and also pointers to objects as values. Depending on which type of variable is used, "." or "->" have to be used respectively.

Peter G.
+1  A: 

[.] operates on a object of a structure. Once a object of a particular structure is declared the [.] operator can be used to directly operate with the members.

[->] operates on a pointer to the object of a structure. This is a dereference operator that is used exclusively with pointers to objects with members. Thus enabling us to access members to the object to which we have a reference.

Based of the declaration you can use these operators.

Praveen S
A: 

The operator a->b, canonically, means (*a).b . So, unlike ".", it will dereference it's first argument.

I could be wrong on this point, but my understand is that it's not "officially" part of C (you specifically mention C in the question). It's a C++ construct that most C compiler vendors have added to C. However, I must admit that I haven't kept up with changes to C, so I could be completely wrong there.

In C++ there are further differences. The "->" operator is overloadable, where as the "." is not.

Scott Wisniewski
JeremyP
Shows what I know about "C".The "*" was a type-o. Thanks.
Scott Wisniewski
A: 

Yes, And . cannot be overloaded but -> can be to have smart pointers for us.

Madhava Gaikwad
Smart pointers? Overloading? In C?
detly
how did i missed that! you guys are talking bout C :(
Madhava Gaikwad