Search code examples
cgccgcc-warning

GCC C warning "duplicate ‘const’ declaration "


Why am I getting

duplicate ‘const’ declaration specifier [-Wduplicate-decl-specifier]

for this declaration?

extern uint8_t CalculateChecksum(const communicationBlock_t const *messageBlock);

Where communicationBlock_t is a struct.

I don't the function to be able to change the structure pointed to by the parameter, nor do I want it to be able to point that parameter elsewhere.

What am I doing wrongly?


Solution

  • It is sufficient to write

    uint8_t CalculateChecksum(const communicationBlock_t* messageBlock);
    

    This means that you can't change the pointed-at contents. If you want to block a pointer from getting assigned to a different address, you would write * const.

    But that isn't necessary here, messageBlock is a local copy of the original pointer and the caller should not care about what that function does with it internally.

    This is a matter of style:

    Some believe that somehow this adds extra safety by keeping the internals of the function const-qualified. They also believe that it somehow makes sense to write things like void func (const int n). Many preaching this style are C++ programmers used to const qualify member functions - which is something else entirely and can't be done in C anyway.

    Others (like me) believe that const qualifying parameters with * const is regarded as needless clutter that confuses the reader, since the function declaration is to be regarded as documentation for the caller of the function, not for the implementer of the function definition.