• Advertisement
Sign in to follow this  

What's the difference?

This topic is 4359 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just saw somebodies signature that reminded me to ask this question. What is the difference between "->" and "."? I'm pretty sure -> has to do with pointers but Why use it if . can do the same thing, or at least that's what I think. for example: You can use these both and they would work the same.. if(cat.brown == TRUE) and if(cat->brown == TRUE) So, in what case would -> be necessary to use?

Share this post


Link to post
Share on other sites
Advertisement
struct test {

int data;
} *one, two;

one->data = 23;
(*one).data = 23; //same as above

two.data = 25; // this is not a pointer!


Hope that clarifies somewhat.



Share this post


Link to post
Share on other sites
You are correct, the -> operator is for use with pointers. The reason (or the only one I can think of) is to be able to easily see if a variable is a pointer or not.

Share this post


Link to post
Share on other sites
Quote:
Original post by Tera_Dragon
You are correct, the -> operator is for use with pointers. The reason (or the only one I can think of) is to be able to easily see if a variable is a pointer or not.

I think Alpha_ProgDes showed what the benefit is...

// quicker to type
ptr->foo();
// slow to type
(*ptr).foo();
// not to mention confusion is avoided...
*ptr.foo( ); // not the same as (*ptr).foo()


Share this post


Link to post
Share on other sites
Correct. In practice, -> takes more work than .. In theory, or at least visually, -> is a pointer.

Kuphryn

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement