top | item 42110159

C Gibberish to English

66 points| warkanlock | 1 year ago |cdecl.org

50 comments

order

ashleyn|1 year ago

   char (*(*x[3])())[5]
I'm more of the mindset that writing something like this is probably a code smell to begin with. Is there any reason I'm not thinking of right now, that this couldn't be typedef'd and refactored into something far more readable?

C gets a lot of blame for pointer gibberish like this but quite honestly you can write gibberish in any language. I don't see any fundamental or technical reason you couldn't write clean, readable C.

danielEM|1 year ago

Why do people continue to use this term "code smell", instead of "hard to read code" or something similar, more equivalent??? First seem almost offensive to an author.

theanonymousone|1 year ago

I'm not a C programmer but your comment reminds me of all the lame jokes people make about the German language.

jacoblambda|1 year ago

the function signature certainly should be type def-ed. i.e.

    typedef char (*fn())[5];
and then you have the original as

    fn x [3];

pavlov|1 year ago

Use typedef?

Granted, the function pointer syntax is forever confusing (to me anyway). The rest is easily tackled by naming things.

Even for function pointers, it’s just one lookup and then you can copy-paste the typedef for any other function pointer types in the project.

sebstefan|1 year ago

>Use typedef?

What if you're given somebody else's code and you need to understand it to put a typedef there

elcritch|1 year ago

Handy site!

Next I want one to explain some of Rust’s more cryptic pointer gibberish. Usually I just hit “use suggested fix X” until the compiler’s happy.

Thiez|1 year ago

Rust isn't so bad, is it? The example of `char ((x[3])())[5]` would translate to `[fn() -> [fn() -> u8; 5]; 3]`. It's inherently an ugly type, but I think it's easier to read than the C version.

makach|1 year ago

I can read that.

I don't think it is gibberish. It's code and in order to read that code you need to understand the language, and to understand language you need learning and experience.

Maybe it can be useful for learning, but if you have to use such tool, I suspect you won't understand it anyway - so in a way it is more a gibberish-to-gibberish translator.

tuveson|1 year ago

I disagree. The conventions for declaring arrays, pointers, and function pointers are all idiosyncratic. In C, the type is always to the left of the variable being declared. Except for arrays, which have part of the declaration to the right. And except for pointers, which need to be affixed to every item if there are multiple declarations. And except for function pointers where you need to wrap the variable name like (*name). Individually I can wrap my head around these exceptions, but putting all of them together, it's just hard to read.

Brian_K_White|1 year ago

It takes a pretty smart person to do that. Which is pretty confusing.

How can such a smart person not not understand how all things that are possible are not all equally good?

The fact that both the compiler and you can parse that doesn't make it a good way to document or convey meaning or intent.

C is chock full of inconsistencies and ambiguities that are only disambiguated by essentially being a human compiler and maintaining the same parsing state-machine manually in your head to know what any given "(" actually means or does. As a self-proclaimed fluent C linguist, you know this better than most.

All coding involves that of course but all implimentations are not equally unhelpful.

The cpu and some people can read the binary itself. They just need to know the cpu's opcodes, documented right in the datasheet that anyone can read.

card_zero|1 year ago

Is there a language that's substantially free of gibberish?

pjmlp|1 year ago

Those of Algol/Wirth linage, or influenced by then.

Then again people complain that they are too verbose, and they rather write in hieroglyph friendly languages.

unrealhoang|1 year ago

for declaring function pointer?

Any language with type after name :

    // c
    char (*(*x[3])())[5];
    // golang
    var x [3]func() *[5]byte

dogleash|1 year ago

Depends on the features you want.

sitzkrieg|1 year ago

forth will contain as much gibberish as you put into it

marginalia_nu|1 year ago

Yeah sure, COBOL is great in that regard. Basically reads as English.

vzaliva|1 year ago

Is it just a web wrapper around good old `cdecl` command? Or it does something different/better?

ngcc_hk|1 year ago

May need some bracket first then English.

djmips|1 year ago

Is this largely supplanted by LLMs?

veltas|1 year ago

The difference is I trust this website, and wouldn't trust an LLM.

marginalia_nu|1 year ago

LLMs are mostly correct with regards to this stuff.

cdecl is always correct with regards to this stuff.

I don't know why you'd choose the former.

andix|1 year ago

Probably.

Output for the example I got on opening the website:

  char (*(*x())[5])()
cdecl.org: declare x as function returning pointer to array 5 of pointer to function returning char

ChatGPT: x is a function that, when called, gives us access to 5 functions that each return a character. (TL;DR, it gave a full explanation too)

Like mentioned before the error rate of LLMs is probably much higher on complex expressions.

lynx23|1 year ago

Why is it that the first thing I try tends to uncover shortcomings?

typedef uint64_t qbb_t __attribute__((vector_size(sizeof(uint64_t) * 4)))

Syntax error

OK, its an extension, meh.

veltas|1 year ago

Looking at the project issues, it seems it supports only 1989-era C, and some Apple stuff, before you even get to attributes.

kmoser|1 year ago

This failed because it doesn't do math: int *x[10/2]