News for hackers. Stuff that matters.
Re:Building a C Compiler Type System – Part 1: The Formidabl... by bluetomcat on Thursday July 07, 2016 @01:00PM (#12050588)
The key issue with C declarations is that "declarations mirror use" which means that you can interpret them this way:

    int *p; // dereferencing "p" gets you an "int"
    int **q; // dereferencing "q" twice gets you an "int"
    char a[10]; // subscripting "a" gets you a "char"
    void (*f)(void); // calling f() gives you "void"
People who write the asterisk next to the type often have a hard time understanding this.
Re:Building a C Compiler Type System – Part 1: The Formidabl... by Someone on Thursday July 07, 2016 @02:21PM (#12051153)
Also, we have

  int *p, q;
which says "p is a pointer to integer; q is an integer". That's an argument against writing

  int* p, q;
because that suggests that both p and q are pointers to integer.

And yes, you can write (IIRC)

  int q, *p;
(See also http://www.stroustrup.com/bs_faq2.html#whitespace)
Re:Building a C Compiler Type System – Part 1: The Formidabl... by Avernar on Friday July 08, 2016 @05:53AM (#12054476)
That's why I declare pointer variables one to a line. Wish the language would work the other way with the asterisk associativity. Mainly because this just looks wrong to me:

  int *p, *q;

  typedef int *intp;
  intp p, q;
That just screams that the * belongs to the type and not the variable name.
Re:Building a C Compiler Type System – Part 1: The Formidabl... by dllthomas on Friday July 08, 2016 @01:31AM (#12053702)
YRC
Re:Building a C Compiler Type System – Part 1: The Formidabl... by tinco on Thursday July 07, 2016 @02:05PM (#12051048)
I think you have it backwards. People who write the asterisk next to the type understand it just fine, we just don't understand how writing it next to the identifier makes sense.

At least, that's how I felt until I read your comment. Now at least I understand why using it that way doesn't drive you absolutely insane. So thanks!

Re:Building a C Compiler Type System – Part 1: The Formidabl... by Mikhail_Edoshin on Friday July 08, 2016 @03:33AM (#12054066)
What about function declarations? If a function returns a pointer, will you stick the asterisk to the function name? And in a declaration you may omit parameter names entirely, so there's nothing to stick the asterisk to:

    Foo* bar(Baz*, Qux*, int, int*, int**);
I myself do it differently depending on the context. In functions I always stick the asterisk to the type, but if I declare variables, I stick it to the name:

    Foo* bar(Baz* baz) {
       Foo *a, b; Baz c, *d;
       ...
    }
Re:Building a C Compiler Type System – Part 1: The Formidabl... by bluetomcat on Friday July 08, 2016 @04:12AM (#12054171)
The same reasoning applies to function declarations returning pointers:

    int *f(void); // "*f()" returns "int" and "f()" returns "int *"
My habit is to always put a space between the type and the asterisk, regardless of whether there is an identifier:

    sizeof(int *);
    x = (char *) y;
Re:Building a C Compiler Type System – Part 1: The Formidabl... by jotux on Thursday July 07, 2016 @03:09PM (#12051466)

    void (*fn)(int*) // fn takes a pointer-to-an-int and gives you void
Wouldn't this suggest that the type this function takes is `int*`?

I've always thought the pointer declaration syntax in C was just an oversight and I get around it's ambitiousness by always declaring variables on their own line.

Re:Building a C Compiler Type System – Part 1: The Formidabl... by bluetomcat on Thursday July 07, 2016 @04:06PM (#12051779)
As the article says, int-star is an "abstract declarator". An abstract declarator simply omits the name, otherwise it is the same. Therefore, the "canonic" way to write that declaration would be:

    void (*fn)(int *);
Re:Building a C Compiler Type System – Part 1: The Formidabl... by paxcoder on Thursday July 07, 2016 @04:53PM (#12052002)
>People who write the asterisk next to the type often have a hard time understanding this.

Why do you think so?

Re:Building a C Compiler Type System – Part 1: The Formidabl... by dllthomas on Friday July 08, 2016 @01:31AM (#12053707)
I've observed similar... amongst early learners of the language. I doubt it persists.
Re:Building a C Compiler Type System – Part 1: The Formidabl... by paxcoder on Saturday July 16, 2016 @12:18PM (#12106824)
Are you saying that early C learners who do not place the asterisk near the type have an easier time understanding the same?
Re:Building a C Compiler Type System – Part 1: The Formidabl... by TickleSteve on Thursday July 07, 2016 @02:07PM (#12051065)
absolutely wrong.

the 'type' is pointer-to-int, the 'name' is 'p', hence 'int* p'.

I understand pointers quite well, thankyou....

Re:Building a C Compiler Type System – Part 1: The Formidabl... by ScottBurson on Thursday July 07, 2016 @02:21PM (#12051155)
> the 'type' is pointer-to-int, the 'name' is 'p', hence 'int* p'.

It would be great if the language actually worked that way, because that's the natural way to think about it. Unfortunately, it doesn't. In the simple case of a single pointer variable, we can put the star next to the type and pretend that C works the way we think, but it's a fiction.

Re:Building a C Compiler Type System – Part 1: The Formidabl... by Gankro on Thursday July 07, 2016 @03:14PM (#12051502)
I don't see how anything they said is incorrect. The type is int*, the name is p. C just has this weirdly optimized multiple value decl syntax that for some reason really wants to make it easy to declare an array of int, pointer to int, and function to int all at once instead of making it easy to declare multiple pointers to int.

It seems to be a perfectly rational reaction to say "oh this was a stupid idea, let's not take advantage of it". (which is honestly a great way to approach a lot of C's features)

Re:Building a C Compiler Type System – Part 1: The Formidabl... by ScottBurson on Thursday July 07, 2016 @06:05PM (#12052372)
I don't really disagree; just remember that you're abusing the syntax, because if you forget, it will abuse you back.
Re:Building a C Compiler Type System – Part 1: The Formidabl... by jotux on Thursday July 07, 2016 @03:20PM (#12051529)
C's comma operator syntax is also weird and, in my experience, many people don't really understand it that well.

edit: In reference to the last line: "oh this was a stupid idea, let's not take advantage of it".

It's a weird feature that generally makes the code more difficult to understand but people still use it a lot, similar to multiple declarations on a single line with pointers mixed in.

Re:Building a C Compiler Type System – Part 1: The Formidabl... by bluetomcat on Thursday July 07, 2016 @03:53PM (#12051689)
The comma acts as a separator in declarations, function calls and initializers.

In ordinary expressions it is a normal binary operator (just like / or &&), but has the lowest precedence and simply evaluates the left-hand side, discarding the result, then (in that order) evaluates the right-hand side, returning its value as the result.

Re:Building a C Compiler Type System – Part 1: The Formidabl... by penguinduck on Thursday July 07, 2016 @03:45PM (#12051652)
Maybe you don't understand it that well because the described usage of a comma is not a comma operator (or any kind of operator).
Re:Building a C Compiler Type System – Part 1: The Formidabl... by imtringued on Friday July 08, 2016 @01:26AM (#12053686)
Re:Building a C Compiler Type System – Part 1: The Formidabl... by penguinduck on Friday July 08, 2016 @01:54AM (#12053778)
Your point? The multiple declaration syntax which is being discussed here uses comma separators, not operators.
Re:Building a C Compiler Type System – Part 1: The Formidabl... by bluetomcat on Thursday July 07, 2016 @02:15PM (#12051122)
Then how about cases like these:

    int* a, *b;
The language was intentionally designed so that the same operators with the same precedence levels are used in declarations as well as in normal expressions.
Re:Building a C Compiler Type System – Part 1: The Formidabl... by bonzini on Thursday July 07, 2016 @02:11PM (#12051098)
And "int* p, q" declares to pointers to int, amirite?
Re:Building a C Compiler Type System – Part 1: The Formidabl... by TickleSteve on Friday July 08, 2016 @03:49AM (#12054106)
no.

You should only ever declare one thing per line.

int* p;

int q;

Re:Building a C Compiler Type System – Part 1: The Formidabl... by bonzini on Monday July 11, 2016 @05:14AM (#12069822)
Only if you don't know how the thing actually works.