2

I want to change

int *i;

to

int* i;

using gnu indent. How can I go about doing that?

If not possible how to at least make kernighan&ritchie style

int * i;

to

int *i;
Necktwi
  • 2,483
  • 7
  • 39
  • 62
  • 10
    In `int* a, b;` What type does the variable `b` has in your opinion? – Algirdas Preidžius Dec 05 '18 at 16:20
  • 3
    "`*i` is of type `int`" is correct. Also `int *i, j;` clearly shows that `j` is not a pointer whereas `int* i, j;` makes `j` look like a pointer. Anyways, consider using `clang-format`. – nwp Dec 05 '18 at 16:20
  • 1
    i agree but what is the actual variable type being created? is it an `int*` or `int`? – Necktwi Dec 05 '18 at 16:27
  • 6
    Is the question here "How do I use gnu indent?" or "Which style is better?" – Drew Dormann Dec 05 '18 at 16:27
  • question is about how to get the desired format. I have to put some additional lines to make stackoverflow happy – Necktwi Dec 05 '18 at 16:29
  • 1
    Did you read the documentation to see if there's an option that controls it? – Shawn Dec 05 '18 at 16:37
  • yeah I checked but couldn't find. what to look for to find it? – Necktwi Dec 05 '18 at 16:37
  • 5
    This question is about getting **the result desired by the OP** from GNU indent. Please do not discuss whether that result is good or bad in your opinion. – DevSolar Dec 05 '18 at 16:38
  • @shawn i've searched the words `astrik`, `pointer` and `dereference` but none or found. please suggest me a keyword. – Necktwi Dec 05 '18 at 16:41
  • 2
    @AlgirdasPreidžius How often do you write code like that? Still don't understand why people keep anchoring themselves to one edge case in this debate. – Lightness Races in Orbit Dec 05 '18 at 17:11
  • @LightnessRacesinOrbit I, personally, never. I only was giving a reason, why some write it like so, to the (now edited out) statement "_I don't understand why many write as `int *i`_". – Algirdas Preidžius Dec 05 '18 at 17:13
  • @AlgirdasPreidžius Okay – Lightness Races in Orbit Dec 05 '18 at 17:13
  • fyi in `C++` it has *always* been recommended that you declare variables individually as and when you use them. Declaring multiple variables in one go has always been considered bad practice. Bunching all declarations together at the top of a function was due to `C` being limited in that way. `C++` never had that restriction. So please don't get in the habit of doing `int a, b, c;` as some suggest here. – Galik Dec 05 '18 at 17:38

4 Answers4

3

I did not find any corresponding option in the GNU indent manual. An alternative would be to use AStyle, which offers the --align-pointer option:

With --align-pointer=type / -k1:

int* a;

With --align-pointer=middle / -k2:

int * a;

With --align-pointer=name / -k3:

int *a;
DevSolar
  • 67,862
  • 21
  • 134
  • 209
2

I believe, gnu ident doesn't have this option. CLang format, on the other hand, seems to have it as PointerAlignment option, which can take following options:

Possible values: PAS_Left (in configuration: Left) Align pointer to the left.

int* a;

PAS_Right (in configuration: Right) Align pointer to the right.

int *a;

PAS_Middle (in configuration: Middle) Align pointer in the middle.

int * a;

More details can be found here: https://clang.llvm.org/docs/ClangFormatStyleOptions.html

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • Should be a comment really, as it does not exactly answer the question... – DevSolar Dec 05 '18 at 16:45
  • But this option is horrible clang-format does not make the difference between the * inside a ptr-declarator and the * of the indirection expression. I don't use clang format just for that! – Oliv Dec 05 '18 at 16:46
  • 2
    @DevSolar well, I think it does, as OP has somewhat like an XY problem. The tool chosen by OP doesn't have this functionality, but there is a replacement tool. I believe, it is a valid answer, which doesn't really fit into comment., – SergeyA Dec 05 '18 at 16:46
  • @Oliv it does not??? I can't believe it! I will test myself and will delete this answer if find it to be true. – SergeyA Dec 05 '18 at 16:47
  • 1
    Valid answers to the question "how do I,,,?" include "you cannot" —unless incorrect. – Tim Randall Dec 05 '18 at 16:48
  • It would be surprising for GNU indent to have that option _for C++_, seeing as its C++ support is very experimental and there are plenty of cases in C++ where you need a bunch of context to even decide whether something is a declaration or not. Templates + dependent vs non-dependent names for example. – Max Langhof Dec 05 '18 at 16:50
  • I'll field AStyle as an answer then as well. ;-) – DevSolar Dec 05 '18 at 17:00
  • 1
    @DevSolar and I believe, this is a valid answer to, which I upvoted :) – SergeyA Dec 05 '18 at 17:04
1

How can I go about doing that?

If not possible how to at least make kernighan&ritchie style

The documentation for GNU indent does not clearly describe any option specifically affecting the whitespace around the asterisk in a pointer declaration, but it does have an umbrella option -kr for requesting K&R style, and I find that that does cause indent to perform the formatting you request, snuggling up the asterisk next to the identifier. Of course it has many other effects, as well, though these can be overridden by additional explicit options.

The -gnu general style option, which is the default, also has this effect. That makes it tricky to sort out which detail option controls this specific behavior, but certainly one answer to your question is that indent will convert your pointer declarations to the K&R style you describe with no options at all.

In fact, as far as I can tell, indent will perform that particular formatting regardless of what options you provide. There does not seem to be any option to modulate that behavior.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
1

Use -pal

From the GNU indent manual:

-pal

--pointer-align-left

Put asterisks in pointer declarations on the left of spaces, next to types: “char* p”.

smac89
  • 39,374
  • 15
  • 132
  • 179
  • It should be noted that this only works for built-in types. All user-defined types (through typedef) should be supplied as arguments to `-T` on the command line, otherwise there will be space between the type and the asterisk. – Vilinkameni Aug 11 '21 at 21:22