logoalt Hacker News

tobyhinloopen • 12/09/2024 • 2 replies • view on HN

My auto-formatter always changes it like that and I just don't understand why `char *foo` is better than `char* foo`.

I consider `char*` to be the type of `foo`. What's the rationale of having `char *foo`?


Replies

onre • 12/09/2024

Dunno how this is supposed to be worded, but it's because the "pointerhood" is tied to the variable being declared, not the type itself. This becomes obvious when you declare multiple variables at once.

  char* cat, dog;  /* one char pointer, one char */
  char *cat, *dog; /* two char pointers */
➕ show 2 replies
eqvinox • 12/09/2024

It's purely cultural. "char* foo" or "char * foo" does make more sense than "char *foo". But culture is a very strong and useful thing; if it "looks weird", that will create issues in people's brain when thinking about the code.

Also, case in point: how do you format "char * const foo"?

➕ show 2 replies