from Hacker News

Cdecl – Turns English phrases into C declarations

by dammitcoetzee on 1/7/19, 5:12 AM with 50 comments

  • by jcranmer on 1/7/19, 6:11 AM

    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 normal parse tree. First we take foo. Then we dereference it (so foo is a pointer). Next we call it as a function with no arguments (so foo is a pointer to a function that takes no arguments). Next, we dereference it again. Then we index into the result as an array. Finally, we reach the end, so we look at what the declared type and find that this type is an int. So foo is a pointer to a function that takes no arguments and returns a pointer to an array of 3 ints.

    You can also use this to go backwards. What's the syntax for a function that takes an integer argument returns a pointer to an array of function pointers taking no arguments and returning integers? Well, we want to take foo, call it, dereference it, then index into an array, then dereference it again, then call it again, then return an int. Or int (* (* (foo)(int))[5])(void).

  • by ridiculous_fish on 1/7/19, 7:14 AM

    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.

  • by saagarjha on 1/7/19, 6:10 AM

    For all of its simplicity, the syntax for complex types in C is pretty horrible. Yes, I know the "inside out" rule, and can usually read these, but that doesn't make it any less bad.
  • by TorKlingberg on 1/7/19, 6:58 AM

    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.
  • by nurettin on 1/7/19, 7:56 AM

    Back in early 2000s, we had bots on IRC doing this. My favorite technique was to pass the type to a template function, assign it to an integer and then parse the compile time error produced by gcc to extract the type.
  • by Eli_P on 1/7/19, 9:31 AM

    If I recall correctly, this one came as an exercise in Knuth's book of C programming, ibidem were C declarations and priorities explained.
  • by pkaye on 1/7/19, 7:45 AM

    Its better to create a series of typedef and build up the declaration. Most of the time you need those sub typedef anyway.
  • by valerij on 1/7/19, 5:34 PM

    on topic of function pointers, is there a template to turn

      std::funtion<foo(bar, baz)>
    
    into

      foo(*)(bar, baz)
    
    ?
  • by mey on 1/7/19, 6:13 AM

    Need this for bash and by proxy regex.
  • by SidiousL on 1/7/19, 7:23 AM

    The actual principle behind the C type declarations is "declaration follows use". Let me explain what this means. Take this declaration

       int *pi;
    
    Means that when I dereference the variable pi, I get an int. This also explains why

       int *pi, i;
    
    declares `pi` as a pointer to `int` and `i` as an `int`. From this point of view it makes sense stylistically to put * near the variable.

    Declaration of array types is similar. For example,

       int arr[10];
    
    means that when I take an element of `arr`, I obtain an `int`. Hence, `arr` is an array of ints.

    Pointers to functions work the same way. For example,

       int (*f)(char, double);
    
    means that if I dereference the variable `f` and I evaluate it on a `char` and on a `double`, then I get an `int`. Hence, the type of `f` is "pointer to function which takes as arguments a char and a double and returns an int".
  • by skookumchuck on 1/7/19, 7:12 AM

    The way to make complex C declarations legible is to use typedefs for the subtypes (like function pointers).
  • by Jerry2 on 1/7/19, 6:18 AM

    Tried it on this gibberish but it complains about syntax:

    ((void(*)(void))0)();

  • by unnouinceput on 1/7/19, 8:15 AM

    tried: declare xxx as integer pointer to array of string equal to "mumu" and "kaka"

    got: bad character '"'...apostrophe instead of double quote has the same result...well, I guess I expected too much