-5

I find that I refer to the value at the memory location pointed to by a pointer far more often then I want to refer to the actual value of the pointer. As a result I wonder why C does not use the asterisk in the to refer to the actual value of the pointer since it is more typing.

I have already read a post on another website about how B did it this way. This does not answer my question; it just changes it to why did B do it this way.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Erk
  • 41
  • 6
  • 1
    Voted to close as primarily opinion based. Have you bothered to read http://stackoverflow.com/help/dont-ask ? – John3136 Aug 26 '15 at 05:00
  • This is kind of an age-old thing. Java dropped asterisks entirely to avoid requiring pointer syntax to access the actual values. – Purag Aug 26 '15 at 05:01
  • @Purag Java has no notion of 'address of' or 'pointer' - why should it bother dropping something it never had a need for? (There are *many* languages with Call by Object Sharing semantics that have never had pointer-asterisk or arrow operators.) – user2864740 Aug 26 '15 at 05:02
  • @John3136 yes, this probably does have a definite answer as there is most likely a logical reason that the language was designed this way. – Erk Aug 26 '15 at 05:02
  • @user2864740 I don't understand what you're saying; the implementation in Java of "references" is the same as a dereference of a pointer in C. Java is in the C family of languages and exhibits such similarities in compiled code. The benefit is that you don't need to deal with pointer syntax. (Benefit to some...restriction to others) – Purag Aug 26 '15 at 05:04
  • @Purag Java carries on a very vague notion of imperative C syntax; and is otherwise quite unrelated. Objects can be treated indirectly without the need for a 'pointer' or discussion of such implementation; while the JLS unfortunately - for such conversations - delves a lot into the specifics of references (which are not pointers) this is mute at the language syntax level itself. As a trivial counter example, consider JavaScript also with a syntax vaguely like C - and yet the ECMAScript specification makes absolutely *no* mention of pointers or reference-values .. let a language be itself. – user2864740 Aug 26 '15 at 05:06
  • @user2864740 right, but what we call references in Java are just pointers; they're far different from C++'s true references. You *are* right, but I think it's still worth mentioning that Java (among other languages) give the behavior of pointers without pointer syntax. At a high level, this isn't significant, but compiled code might say otherwise. – Purag Aug 26 '15 at 05:08
  • @Purag No, there is no difference in the 'compiled [byte-]code' of Java. No pointers. The JLS does not define in what method a JVM wishes to implement it - and there are many different JVM implementations, including those in languages which themselves lack pointers. Just as it is meaningless, given the context the knowledge, to point out that references in Java (perhaps more aptly-termed 'reference-values') and and C++ are different concepts, so is it meaningless to try and argue that Java has - or requires - pointers. – user2864740 Aug 26 '15 at 05:09
  • @user2864740 That's my point. CPU architectures have "dereference" instructions. Java bytecode gets interpreted to machine code that dereferences the memory locations in Object references at runtime. Again, at a high level this is trivial, but it's not a useless comparison. I think it's unfair to say that "Java uses pointers without pointer syntax" is untrue. – Purag Aug 26 '15 at 05:14
  • 2
    @Purag Java does not *have* pointers. How then can Java use pointers? If Java does not have pointers, why would it it need the same operator(s) are C, which does? Please open up a copy of the [Java Language Specification](https://docs.oracle.com/javase/specs/) and search for "pointer". You will find many mentions of the poorly-named NullPointerException (thankfully the CLR did not follow this) and one sentence of prose wording to describe the behavior. – user2864740 Aug 26 '15 at 05:15
  • @user2864740 In the way that `pointer -> function()` and `(*pointer).function()` are available in C, you have `object.method()` in Java. What you can do with references in Java are of course a strict subset of the functionality of pointers in C, but the similarity is there. There is an item living somewhere in memory with properties that can be accessed by *following* its address; in C, this is a dereference. In Java, this happens implicitly with member accesses. But the machine code to do it is a memory access all the same. – Purag Aug 26 '15 at 05:18
  • @Purag But there *are no pointers in Java*. And thus there is *no need for that C syntax*. Java didn't "drop" - Java *never had*, because it *doesn't require it* because .. dah-dah-dum, *no pointers*. – user2864740 Aug 26 '15 at 05:19
  • @user2864740 perhaps "Java gives the *functionality* of pointers without the pointer syntax" is a better description. I'm not arguing that you can use pointers in Java, that's ridiculous. – Purag Aug 26 '15 at 05:19
  • @Erk The reason why it's defined this way is that your proposed syntax wouldn't make sense, or at least it would be **largely** inconsistent. For any object `x`, if I write `x` in the code, I mean "the object x". This should be true for all types. Now if you make pointers a special case, then writing `x` in the code doesn't mean `x` anymore, which is bad. – The Paramagnetic Croissant Aug 26 '15 at 05:22
  • @user2864740 Oh, okay you're just picking at my semantics. I don't mean "once Java had this but they thought it sucked so they dropped it." I mean, as a derivative of C, Java took this capability of the language (accessing data stored in remote memory locations) but simplified it by defining its own syntax that didn't use asterisks. – Purag Aug 26 '15 at 05:24
  • @user2864740 Check [this](http://javadude.com/articles/passbyvalue.htm) out. Note especially the "Pointers vs. References" section. By the way, the [JLS](http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.3.1) explicitly describes references as pointers. ;) – Purag Aug 26 '15 at 05:40
  • It's just like Pascal uses `^` to dereference a pointer – phuclv Aug 26 '15 at 06:02

1 Answers1

0

I doubt anyone can tell you why it was designed the way it was. But a pointer is a variable that holds an address. Therefore, it's value is the address. I would find it strange and unexpected if it required special syntax to get the value of any variable.

Note that C++ lets you use references to access the value pointed to without special syntax. But you are dereferencing the pointer, so for me it makes perfect sense to require a different syntax.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466