10

Possible Duplicate:
Arrow operator (->) usage in C

I am trying to figure out the difference between the "." and "->" styles of data access in C language. Eg.

struct div{
    int quo;
    int rem;
};

1) Using "->"

struct div *d = malloc(sizeof(struct div));
d->quo = 8/3;
d->rem = 8%3;
printf("Answer:\n\t Quotient = %d\n\t Remainder = %d\n-----------\n",d->quo,d->rem);

2) Using "."

struct div d;
d.quo = 8/3;
d.rem = 8%3;
printf("Answer:\n\t Quotient = %d\n\t Remainder = %d\n-----------\n",d.quo,d.rem);

I am getting the same output for both the cases.

Answer: Quotient = 2 Remainder = 2

How these two approaches are working internally? And at what time which one should be used? I tried searching it on the internet but of not much help. Any relevant link is also appreciated.

Also is there any difference between their storage in memory?

Community
  • 1
  • 1
Atul Gupta
  • 207
  • 1
  • 5
  • 17
  • There is a perfect answer for this question somewhere around _StackOverflow_, let me just find it now... – K-ballo Dec 30 '12 at 00:40
  • [Here's another which explains](http://stackoverflow.com/questions/1238613/what-is-the-difference-between-the-dot-operator-and-in-c) difference in C++ context (C part is still there). – P.P Dec 30 '12 at 00:47
  • You answer lies there in your question itself. It is not between `.` and `->` but the context in which data is accessed. – NeonGlow Dec 30 '12 at 04:31

7 Answers7

27

Basic difference is this:

. is the member of a structure
-> is the member of a POINTED TO structure

So the . is used when there is a direct access to a variable in the structure. But the -> is used when you are accessing a variable of a structure through a pointer to that structure.

Say you have struct a:

struct a{
    int b;
}

However say c is a pointer to a then you know that: c = (*a). As a result -> is being used as a alternative for a certain case of the .. So instead of doing (*a).b, you can do c->b, which are the exact same.

Rivasa
  • 6,510
  • 3
  • 35
  • 64
12

-> is defined in terms of .

That is, a->b is equivalent to (*a).b and so you'll obviously get the same results.

-> exists for convenience.

Pubby
  • 51,882
  • 13
  • 139
  • 180
3

When dealing with pointers to structs, use -> and when using them directly, use ..

3

Use the first approach when you need to manually manage the lifetime of the created object. Use the second approach when the lifetime of the created object is limited to the scope in which it is created or the scope of the object that contains it.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Is there any difference between their storage in memory? – Atul Gupta Dec 30 '12 at 01:34
  • @AtulGupta: That's up to the implementation. Generally, `new` will allocate on the heap, declaring an object in a particular scope will allocate what's known at allocation time on the stack and anything allocated dynamically later on the heap. – David Schwartz Dec 30 '12 at 04:28
  • What I understand is that in the case (1) memory for struct div *d will be allocated on the heap since I am using malloc. Whereas in the case (2) it will be allocated on the stack. Please correct me if I am wrong. – Atul Gupta Dec 30 '12 at 04:38
  • "On the heap" is imprecise, ambiguous, implementation-specific language that's widely used in place of the correct phrase "allocated storage duration". See "6.2.4 Storage durations of objects" and "7.20.3 Memory management functions". – R.. GitHub STOP HELPING ICE Dec 30 '12 at 04:41
3

In C, -> is very similar to ., but deals with pointers. In a structure, the . is used to reference directly to a member within the structure, while -> is used to reference the member of a pointed to structure. You get the same pretty much the same result using pointers or not, depending on the situation.

a -> b is the same as (*a).b

Think of it like a = a + 1. For simplicity and convenience, a++ is used to accomplish the same exact outcome.

syb0rg
  • 8,057
  • 9
  • 41
  • 81
2

In simple words it is expected to give the same result, what you do is to change the memory allocation and how to access data from a structure. Some of good links about what you want to understand is an tutorial about dynamic memory allocation and Working with Dynamic Memory in C++.

Mihai8
  • 3,113
  • 1
  • 21
  • 31
2

when . is used, it is termed as structure to member..

and when -> is used, it is termed as pointer to member..

how ever if pointer to member is used, it can be resolved like this..

x -> y and (*x).y are both the same..

Raghu Srikanth Reddy
  • 2,703
  • 33
  • 29
  • 42