I've been stumbling upon this constantly.

Suppose that I have a function that accepts a structure, or a pointer to a structure:

struct s {
    int n;
    char c;
void f(struct s *s);

I'd like to pass an anonymous structure as the argument to f:

f(&(struct s) { .n = 5, .c = 'x' });

This works fine, but the problem appears when I try to split the structure literal into several lines.

By default, Emacs indents that as:

f(&(struct s) { 
        .n = 5, 
            .c = 'x'

instead of the expected

f(&(struct s) {
    .n = 5,
    .c = 'x'

The only tweaks regarding C in my .emacs file are

(setq-default indent-tabs-mode nil)
(setq c-basic-offset 4)

(other than that, I work with hs-minor-mode)

Is there a way to fix the indentation for this use case?

(I am guessing, that Emacs confuses structure instantiation with the use of the "comma operator" which is used to group expressions; However, I rarely use the comma operator and I have no expectations regarding the indentation, so I wouldn't mind if the rules for this operator were overridden)

  • FWIW, sm-c-mode seems to indent this almost exactly like you want (just one column deeper in my quick test). – Stefan May 12 '20 at 4:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.