top | item 39579997

(no title)

jevoten | 2 years ago

> The way C handles pointers, for example, was a brilliant innovation;

How were pointers handled before?

discuss

order

globular-toast|2 years ago

What he's talking about is pointer arithmetic. In assembly you have to "know" what is being pointed at such that you can access an element in an array or a component of a composite type, for example. The computer only knows about bytes. In C you can just do stuff like `(p + 6)->t` and the compiler knows how many bytes "+6" is because it knows what `p` is pointing at.

layer8|2 years ago

Like integers that you could decide to interpret as a memory address and thus use like a char * (char pointer). As in machine code, basically.

C made pointer arithmetics work like array indexing. Also, the combined dereferencing and member lookup operator (->).

kazinator|2 years ago

> Also, the combined dereferencing and member lookup operator (->).

That's actually a dud. The motivation was likely caused by the silly choice of the dereference operator being unary/prefix, requiring parentheses in (*ptr).memb.

Compare with Pascal's ptr^.member where no parentheses are required.

But ptr.memb can just work as well as value.memb. The operator statically knows whether the left operand is a pointer to a struct/union or a struct/union value.