Search code examples
cpointersdeclarationindirection

Is there a convention for pointer declarations in C?


When declaring pointers in C, there are 3 variants:

Variant A:

int* ptr;

Variant B:

int *ptr;

Variant C:

int * ptr;
  • In A, the indirection operator has been appended to the type.
  • In B, the indirection operator has been prepended to the variable.
  • In C, the indirection operator stands freely in between type and variable.

The way a pointer is declared differs depending on the type of documentation I read. Some authors seem to have a preference for certain variants, others use several.

  • Am I correct to assume that there is no difference in functionality between the different variants?
  • If yes, is there a convention for which variant one should be using in C?

Solution

  • There is absolutely no difference in functionality between

    int* ptr;
    

    and

    int *ptr;
    

    Which you use is up to you, there are multiple conflicting coding styles to choose from.