• Advertisement
Sign in to follow this  

Question about dereferencing syntax in C/C++...

This topic is 1653 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 have always accepted how C/C++ dreferences structs and pointers to structs until a recent thread on a new language, C-UP. The developer stated that the language doesn't use the '->' operator to dereference pointers.

This makes a lot of sense. I'm curious why the C++ standard still requires the use of '->' and '*'?
 
struct foo{
int i;
int *j;
};
 
foo foo1,*foo2;
int temp;

// These next lines do the exact same thing...
foo1.i = 1;
foo2->i = 1;
temp = foo1.i;
temp = foo2->i;
temp = *foo1.j;
temp = *foo2->j;
The only reason I can see for the disparity is that '->' and '*' informs the reader that a pointer is being dereferenced. Does it truly matter? All that is being done is accessing a member of a class or struct. Edited by MarkS

Share this post


Link to post
Share on other sites
Advertisement


I'm curious why the C++ standard still requires this?

 

Because that's what C did, and changing the standard now would break literally billions of lines of code.

 

As to why C made the distinction in the first place, well that's another question, but I'd imagine the answer is that when C was written, yes, it truly mattered.

To be fair, there are probably still some instances where it still matters (tight loops, HPC, embedded, etc)

Share this post


Link to post
Share on other sites

Historical C cruft. K&R C was a hell of a language.

 

Because that's what C did, and changing the standard now would break literally billions of lines of code.


 

But there's no reason they couldn't just deprecate it.

Edited by orangecat

Share this post


Link to post
Share on other sites

I'm always perplexed that this is seen as an issue.


It isn't so much of an issue as unnecessary. It is a distinction that doesn't seem to fulfill a need.

Share this post


Link to post
Share on other sites

I'm always perplexed that this is seen as an issue.

It isn't so much of an issue as unnecessary. It is a distinction that doesn't seem to fulfill a need.

I think dereferencing operators are kind of necessary in a language that has pointers. It's a useful abstraction upon which iterators, for example,were built.

If I remember correctly, in some early version of C (or some precursor) you had ty type (*p).foo instead of p->foo, so it's a kind of syntactic sugar. Whether plain dot should also work is almost a matter of taste (except for what Brother Bob said above), but I quite like being able to just read the code and see that a pointer is being dereferenced.

On the other hand, I am not so keen on having someone deprecate -> and cause me thousands of hours of busywork for no gain at all. I think one of the main reasons behind the success of C++ is that it almost never deprecates stuff. The last thing that people want is to have to grab all the code they already have written and tested and change it. There really are better things to do.

Share this post


Link to post
Share on other sites


But there's no reason they couldn't just deprecate it.

 

Sorry, no. There are heaps of reasons they couldn't deprecate it.

 

For a start deprecation is more commonly done with libraries, not language elements and in order to deprecate something, you must have an alternative to replace it with.

What would you replace "->" with? 

Share this post


Link to post
Share on other sites

What would you replace "->" with?


'.'

This was just a question that I have been pondering since learning of C-UP and its features. It just seems kind of odd, but it has been made more clear since I asked. I still do not feel it is entirely necessary, but I understand why it is kept.

Share this post


Link to post
Share on other sites

 

What would you replace "->" with?


'.'

This was just a question that I have been pondering since learning of C-UP and its features. It just seems kind of odd, but it has been made more clear since I asked. I still do not feel it is entirely necessary, but I understand why it is kept.

 

And once again, you will break literally billions of lines of code.

 

As SiCrane mentioned, you'd break smart pointers and you'd also break most iterators too. 

Share this post


Link to post
Share on other sites


I highly suggest you read this. I won't quote it all here. In short, one of the earliest versions of C was toying around with some special use cases for ->, which were later changed/removed in later versions of C, making -> no longer super special, but they didn't remove ->, and since C++ grew out of C (and tried to be backwards compatible with it), -> stuck.

 

That was fascinating. That offset stuff was insane :)

Share this post


Link to post
Share on other sites

* exists to dereference the pointer. It is indeed very necessary. Sometimes you want to work with the value of the address, and sometimes you want to work with the value pointed to by the address. You need some way to distinguish between the two, and dereferencing with * does just that. It's not just a readability thing. It's a usability thing.


This I know. Must have been a brain fart. I have no idea why I mentioned this in the original post.

-> exists mostly for historical reasons, and has stuck around. I highly suggest you read this. I won't quote it all here. In short, one of the earliest versions of C was toying around with some special use cases for ->, which were later changed/removed in later versions of C, making -> no longer super special, but they didn't remove ->, and since C++ grew out of C (and tried to be backwards compatible with it), -> stuck.


Thank you. That link was very helpful. It is one of those things that do not truly matter, but is odd if you don't know the reason behind them.

Share this post


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

  • Advertisement