logoalt Hacker News

gnramirestoday at 5:35 PM5 repliesview on HN

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".


Replies

kpstoday at 6:21 PM

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

PhilipRomantoday at 7:31 PM

I think the only significant mistake was having both prefix and postfix operators for types. If they all neatly sat on one side there would be no problem.

jandresetoday at 5:55 PM

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.

show 1 reply
dnauticstoday at 5:45 PM

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

show 1 reply
delta_p_delta_xtoday at 6:05 PM

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