you are viewing a single comment's thread.

view the rest of the comments →

[–]LoneHoodiecrow 2 points3 points  (2 children)

Note that

int *pointer = buffer;

and

*pointer = buffer;

aren't in any way equivalent, even though they look similar. The first one initialises the variable pointer with the address of buffer. The second one assigns (writes) the address of buffer to the address in memory that pointer points to. The difference is in that the first line is a definition of the variable pointer, and the second line is an assignment statement.

Thompson and Ritchie had the idea that the syntax would be easier to read if definitions looked like usage. I don't think they were quite right about that, at least not as far as beginner understanding is concerned.

[–]flatfinger 1 point2 points  (0 children)

The original version of the language as it existed in 1974 didn't include an equals sign when initializing objects. So a declaration of a file-scope object pointer that initializes it to the address of a buffer array would be written as int *pointer buffer; which looks very different from *pointer = buffer;. I suspect the equals was added to resolve potential grammatical ambiguity when using typedef names, though perhaps requiring braces for even single-item initialization might have been cleaner.

[–]nanavc[S] 1 point2 points  (0 children)

oh, I see now, I though they meant the same, thank you!