Remix.run Logo
gnramires an hour ago

I've written some C code recently, and it came to me perhaps the pointer syntax may not be ideal. I'm not sure what the ideal would be, but I think a different notation for usage and declaration could make it less confusion.

In particular I associate '*' (used as *ptr, i.e. content that ptr points to), with content, as opposed to '&' (from &var, address of var), so again '*' means content thing points to. But in declaration, when you declare 'char *ptr', which is a pointer to a char, you clearly can't read it exactly the same way ("char with content of a pointer"? More like, the content of a pointer is char). So maybe another symbol like @ (denoting "is a pointer"), or just the keyword pointer, might make things clearer, so you'd have 'char pointer ptr' (ptr is a pointer to a char, read backwards) or simply 'char @ ptr'. The shorter '@' would be justified when you have multiple pointer e.g. when working with multidimensional arrays (which are often @@@float, something like that). Just an idea that occurred me ;)

(Although I hadn't thought about pcfwik's principle that it's written as used, that makes somewhat more sense to me)*

Edit: Said otherwise, in usage syntax the convention (or at least my way of thinking) may be left-to-right, "content of" or "address of", while in declaration we read right-to-left, "is an int", or "is a pointer", and it would make sense to me that the symbol for "is a pointer" is different than the symbol for "content of"/"address of".

kps 12 minutes ago | parent | next [-]

The reading of `char *p` is: The following things are `char`: `*p`.

jandrese 38 minutes ago | parent | prev | next [-]

I thought it was a bit of a miss that C didn't use ^ as the pointer sigil. I mean it's literally pointing. I'm guessing some early terminals didn't have that character on the keyboard.

delta_p_delta_x 27 minutes ago | parent | prev | next [-]

Borrow from the late 1990s upstart GC languages: Pointer<Type>.

dnautics an hour ago | parent | prev [-]

i find Zig does it fairly well. there is also no ambiguity between pointer and multiply.

Joker_vD 29 minutes ago | parent [-]

Yeah, they took it from Pascal:

    var p: ^integer, i: integer;

    p := @i;
    p^ := 42;
Which follows an obvious "if modifier of a base type goes to the left of the type, then the operator that uses this modifier goes to the right in the expression". Just like "array of T/[]T" translates into "arr[index]".