Live data from Hacker News

Cdecl – Turns English phrases into C declarations

cdecl.org

21–30 of 56 posts

Re: Cdecl – Turns English phrases into C declarations

#21
post #3

Here's an easy way to understand how these things work: in C, the type of a pointer/function/array mess is declared by how it's used. For a declaration like "int ( * ( * foo)(void))[3]", you can read it as "for a variable foo, after computing the expression ( * ( * foo)(void))[3], the result is an int." So one way to read C "gibberish" is to ignore the type at the beginning and parse the rest as an expression like a…

How about just using Ada? It has the added bonus of not being a gimmick (depending on who you ask I suppose ; )

Ada: type Ret_Typ is array (1..3) of Integer; Foo : access function return not null access Ret_Typ := null;

C: int ((foo)(const void *))[3]

Cdecl: declare foo as pointer to function (pointer to const void) returning pointer to array 3 of int

Re: Cdecl – Turns English phrases into C declarations

#22
post #6

Tried it on this gibberish but it complains about syntax: ((void(*)(void))0)();

Besides not being a declaration it really is gibberish - calling a null pointer is undefined behaviour. I believe the correct way of calling a function at address zero is ((void()(void))(intptr_t)0)(); which is merely implementation defined.

C has the weird thing that a literal zero in a pointer context becomes a null-pointer which may not actually have the bit pattern 0. And when the optimizer sees a guaranteed null pointer it tends to optimize the whole branch away since entering that would be UB. So if you try calling address zero with "((void()(void))0)();" you might end up with the whole function optimized away as well as any function it gets inlined into.

Re: Cdecl – Turns English phrases into C declarations

#23
post #3

Here's an easy way to understand how these things work: in C, the type of a pointer/function/array mess is declared by how it's used. For a declaration like "int ( * ( * foo)(void))[3]", you can read it as "for a variable foo, after computing the expression ( * ( * foo)(void))[3], the result is an int." So one way to read C "gibberish" is to ignore the type at the beginning and parse the rest as an expression like a…

How about just using Ada? It has the added bonus of not being a gimmick (depending on who you ask I suppose ; ) Ada: type Ret_Typ is array (1..3) of Integer; Foo : access function return not null access Ret_Typ := null; C: int ((foo)(const void *))[3] Cdecl: declare foo as pointer to function (pointer to const void) returning pointer to array 3 of int

In the interest of furthering annoying language smuggery, the rough Rust equivalent:

    foo: fn() -> Box
Alternately, if the pointer is into static memory and not something allocated on the heap:

    foo: fn() -> &'static ;
That's pretty nice to look at and not too hard to read. In my opinion, for commonly used syntax (like fn decls), some well-chosen punctuation marks (', ->, :, in this case) are often boon to readability compared to keywords. So I think the Rust syntax in this case is nicer than Ada's.

But in any case, while complicated C declarations may be uglier and take more effort to read than those in other languages, they are at least tractable once you learn the trick of "declaration follows use" and working backwards as GP describes.

Separately, though, what do you mean by your "gimmick" comment?

Re: Cdecl – Turns English phrases into C declarations

#24

I've been a professional C programmer for years, but I rarely find cdecl useful (command line or website). Not because complex C declarations are intuitive to me, but because cdecl fails on any unknown types. Real world C code is full of typedefs.

Could you not substitute in a known type, get the result and insert the unknown type back in afterwards?

Re: Cdecl – Turns English phrases into C declarations

#25

Hey, this is my site, first published 2009! This is the venerable cdecl enhanced with blocks support. It used to be a shared host with a PHP script shelling out to the cdecl executable, written in K&R C. Now it's that same executable running on AWS Lambda. Yes Lambda really will run arbitrary ELF binaries.

Nice work.

Could you run it as a preprocessor macro?

Re: Cdecl – Turns English phrases into C declarations

#27

Hey, this is my site, first published 2009! This is the venerable cdecl enhanced with blocks support. It used to be a shared host with a PHP script shelling out to the cdecl executable, written in K&R C. Now it's that same executable running on AWS Lambda. Yes Lambda really will run arbitrary ELF binaries.

Thanks for creating it, it really helps with learning C.

Re: Cdecl – Turns English phrases into C declarations

#28

Hey, this is my site, first published 2009! This is the venerable cdecl enhanced with blocks support. It used to be a shared host with a PHP script shelling out to the cdecl executable, written in K&R C. Now it's that same executable running on AWS Lambda. Yes Lambda really will run arbitrary ELF binaries.

Didnt know what blocks are , it seems it's an apple extension.
Post reply on HN