Note: The Listed platform will permanently shut down December 31, 2026. Your data published on Listed will still be available in your personal Standard Notes account. Learn more

The Reasons to Put Asterisk Next to Type Name Instead of Variable Name in C/C++

These variable declarations are both syntactically correct in C/C++:

// First way
char* my_var;

// Second way
char *my_var;

However, the first way should be preferred, for these reasons:

  • **my_var** is a pointer, it’s not a regular variable
  • **char*** is a type: char pointer, a type is not a part of the variable name, for example:
    • char* my_var in which my_var is a variable, asterisk is a part of type
    • int my_var without space will be intmy_var and this is wrong
  • ***my_var** as a whole is understood as a regular variable, my_var pointed to.
  • Things will look less confusing when a pointer is a parameter to a function, and it is passed by reference:
    • **function f(char* &my_param)**, where:
      • **char*** is a type
      • **my_param** is a parameter to function
      • **&** symbol indicates that my_param is reusing address in stack instead of creating new entry in stack
    • **function f(char *&my_param)**:
      • ***&my_param** is very unclear to read it out
  • **vector<char*>** is a very clear template name, a vector of char pointers
  • **vector<char *>** is unclear, why space in between
  • Assignment confusion (box is example class):
    • box* My_Box = new Box(); looks OK because ‘new‘ will return a pointer and My_Box is a pointer
    • box *My_Box = new Box(); doesn’t look OK because ***My_Box** as a whole is a regular variable and assigned with a pointer; this is wrong.
  • Return type problem:
    • **char* my_func(){}** is a function returning a pointer to char
    • **char *my_func(){}** is confusing to look on the left side of space
      • It seems returning char
      • It seems wrong since function name can only start with letter

You'll only receive email when they publish something new.

More from Arno
All posts