Pointer as a Type or Different Roles of the Asterisk in Clang

Date: 12/2/2023 · Tags: #clang, #functional-programming

Which style do you prefer to declare a variable in C?

  1. int *name
  2. int* name

Rasmus Andersson (@rsms) open a thread 1 in X to discuss the history of the convention of put asterisk symbol (*) (also known as unary operator) as a pointer type attched to the name (style 1) rather than the type (style 2).

Nowadays, after a few years of experience as a programmer, I prefer style 2. It's more practical and easier to read in the functional programming way.

There are also many quite interesting comments and supplementary materials in the thread about deferencing operator 234, historical choices 5 and personal flavor 6. Worth to dig in.

Only one critical warning to pick the second style

Avoid declaring more than one variable per statement! -- Phil Eaton (@eatonphil) 7

That means DO

int* foo;
int* bar;

and DO NOT

int* foo, bar;

Footnotes

  1. Origin thread from @rsms

  2. Comment from @rsms

  3. StackOverflow - How to explain C pointers (declaration vs. unary operators) to a beginner?

  4. Comments from @dpc_pw

  5. Bell Labs - The Development of the C Language

  6. Comment from @eatonphil