Please note that the following code is pretty meaningless, I just wanted to reproduce an error that I am seeing in a much more complex codebase. Obviously, I would not create a variable with global scope to pass it to a function that is used in only one file in which the variable resides.
I'm running PC-Lint 9.00L.
In the following example, PC-Lint complains about a redeclaration:
example2.c 18 Error 18: Symbol'testFunction(const struct AnotherExample_t *)' redeclared (Arg. no. 1: qualification) conflicts with line 21, file example.h, module example1.c
Here is the code:
example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
#include <stdint.h>
typedef struct
{
volatile uint8_t item1;
volatile uint8_t item2;
} Example_t;
typedef struct
{
Example_t * p_items;
uint8_t something;
uint16_t somethingElse;
} AnotherExample_t;
extern AnotherExample_t g_externalVariable;
extern void testFunction (AnotherExample_t const * const p_example); //line 21
#endif
example1.c
#include "example.h"
#include <stdio.h>
int main(void)
{
g_externalVariable.something = 5;
(void)printf("%d", g_externalVariable.something);
testFunction(&g_externalVariable);
return 0;
}
example2.c
#include "example.h"
#include <stdio.h>
static Example_t p =
{
.item1 = 0,
.item2 = 1,
};
AnotherExample_t g_externalVariable =
{
.p_items = &p,
.something = 2,
.somethingElse = 3,
};
void testFunction (AnotherExample_t const * const p_example)
{ // Line referenced in lint (line 18)
(void)printf("%d", (int)p_example->somethingElse);
}
Why is lint throwing this error?
THINGS I ATTEMPTED
I noticed that when I remove the declaration of const AnotherExample_t
that the complaint goes away. i.e. -
extern void testFunction (AnotherExample_t * const p_example); //example.h
void testFunction (AnotherExample_t * const p_example) //example2.c
{
...
}
I also attempted to cast the call from example1.c just to see if that changed anything:
testFunction((AnotherExample_t const * const)&g_externalVariable);
That did not change anything.
In both cases, I get an Info 818 message as well:
example2.c 20 Info 818: Pointer parameter 'p_example' (line 17) could be declared as pointing to const
Minimal Reproducible Code
This also results in the same error.
example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
extern void testFunction (const char * const p_example);
#endif
example1.c
#include "example.h"
#include <stdio.h>
int main(void)
{
char testValue = 'c';
char * p_testValue = &testValue;
testFunction(p_testValue);
return 0;
}
example2.c
#include "example.h"
#include <stdio.h>
void testFunction (const char * const p_example)
{
(void)printf("%c", p_example);
}