开发者

How do the "->" and "." member access operations differ in C

I have looked at some resources to tell me how -> and . are different, but they seem to do the same thing. Does -> act li开发者_运维知识库ke a dot operator on a struct?


. 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.


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;


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


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

struct Data data;
data.content = 1;

struct Data* pData = &data;
pData->content = 2;


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);
}


[.] 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.


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;


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.


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.


both used in C++ to access the members of a class. But

  • . is not overloadable,
  • is overloadable

Here's an example showing how you can use both of them:

#include<iostream>
class A {
   public: int b;
   A() { b = 5; }
};

int main() {
   A a = A();
   A* x = &a;
   std::cout << "a.b = " << a.b << "\n";
   std::cout << "x->b = " << x->b << "\n";
   return 0;
}

    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜