In my simple C99 project, I have an external C library that defines the interface (via GObject interfaces ) that I need to implement:
void interface_function (const char *address, [...]);
Now, as part of the implementation (which I need to write), I need to call some other functions from another library (therefore, I cannot change them), to which I need to pass *address , but their method signature skip the const keyword:
void some_api_function (char *address, [...]);
Now, if I just pass *address to some_api_function , I will get a compiler warning:
warning: passing argument 1 of 'some_api_function' discards 'const' qualifier from pointer target type [enabled by default]
I tried explicitly casting to char * in a function call as follows:
`some_api_function ((char*) address, [...]) { ... }
but then I get another warning:
warning: cast discards '__attribute__((const))' qualifier from pointer target type [-Wcast-qual]
The problem is that this is a larger C project, in which many people work, and the policy is that -Werror included, and no warnings about code compilation are received in the mainline source code.
I canโt change the definition of the interface, because itโs from a third party, and I also canโt change the API definitions of external libraries. I know that *address does not change in the external library (it can be const, but, as I said, I can not change this)
I also know that to solve this problem, I could just create a copy of const char* in char * , but this does not seem right to me because it is due to an unnecessary copy operation.
So what is an elegant or โrightโ way to do this?