0

With GNU indent is there a way to put the "star" right after the type?

For example:

void* foo(int* a)

but keeping it near the var in a declaration like

int *a, b;
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Roberto Aureli
  • 1,428
  • 2
  • 12
  • 23
  • 2
    Don't mix pointers and non-pointers in a single declaration. That avoids most of the issue. The `*` denoting pointer is part of the declarator, which is the part after the type and storage classes and so on. – Jonathan Leffler May 12 '18 at 08:12
  • Are you open to using other code formatting tools? I don’t know much about Indent, but `clang-format` at least allows you to always put it to the left (I don’t think it allows switching based on context, though). – Daniel H May 12 '18 at 08:19
  • I've an Emacs macro that format the code calling indent so I think that I easly can switch to another command, thanks! – Roberto Aureli May 12 '18 at 08:21

1 Answers1

2

C standard doesn't say anything about keeping * immediately after type or before variable name, both are fine.

int *ptr1, ptr2;/* valid, ptr1 is pointer variable, ptr2 is normal variable */
int* ptr1, ptr2;/* valid, ptr1 is pointer variable, ptr2 is normal variable */

Similarly in function declaration or definition for e.g

void* foo(int* a) { 
/*...*/
}

or

void* foo(int *a) { /* a is int pointer */ 
/*...*/
}

Read this Placement of the asterisk in pointer declarations

Achal
  • 11,821
  • 2
  • 15
  • 37
  • So I can choose, perfect. What are the option to have the first type of style? – Roberto Aureli May 12 '18 at 07:57
  • 1
    Yes but it doesn't say how to configure GNU Indent to have the start in a specific place – Roberto Aureli May 12 '18 at 08:03
  • 3
    Keep the `'*'` with the variable, avoid confusion, e.g. `char* a, b, c;` certainly does not declare 3 pointers. (1 pointer and 2 char) Much clearer as `char *a, b, c;`. – David C. Rankin May 12 '18 at 08:06
  • 2
    @DavidC.Rankin: And one variable per declaration is clearer still: `char *a; char b; char c;` (or, at least, `char *a; char b, c;` so separate types are declared separately). – Jonathan Leffler May 12 '18 at 08:10
  • 1
    @JonathanLeffler - no quarrel with that. (also there is no `gnu-indent` option for placement of the `'*'`) – David C. Rankin May 12 '18 at 08:11
  • oh ok, so neither an "external module"? – Roberto Aureli May 12 '18 at 08:13
  • No, I just went through `man indent` and there is absolutely nothing that references the placement of the `'*'` -- sorry. – David C. Rankin May 12 '18 at 08:14
  • @RobertoAureli See this https://www.gnu.org/software/indent/manual/html_chapter/indent_1.html#SEC10 – Achal May 12 '18 at 08:20
  • I will add two cents. In C the style used in standards (e.g. [C99](http://www.dii.uchile.cl/~daespino/files/Iso_C_1999_definition.pdf) is `int *ptr;` as to denote, that the expression `*ptr` has the type of `int` (that was K&R intention). In C++ they use `int* ptr;`, see e.g.[cppreference](http://en.cppreference.com/w/cpp/language/pointer). – KamilCuk May 12 '18 at 08:31