tags:

views:

34

answers:

2

Hi,

given the following code:

/* signatures */
int getParams(char params[MAX_PARAM_LEN][MAX_LINE_LEN]);
int getVersion(const char params[MAX_PARAM_LEN][MAX_LINE_LEN],
               const char* tagName );
/* initializing */
char params[MAX_PARAM_LEN][MAX_LINE_LEN] = {};

/* getting parameters */
paramCount = getParams(params); /* OK, params match with getParams signature */

/* processing the params array */
i = getVersion(params, "version"); /* warning: passing arg 1 of `getVersion' from incompatible pointer type */

I see that the constness is the problem, but I don't know why or how to avoid it. What I want is a function which can't modify the params anymore. Any advice is welcome(besides disabling this warning or deleting const in the processing function).

Thanks: Visko

+2  A: 

You can't eliminate these warnings in C without making an explicit cast to the proper type. Without a typedef this is going to look ugly though

i = getVersion((const char (*)[MAX_LINE_LEN]) params, "version")

This is a strange quirk specific to C language. In C++ this issue was fixed.

BTW, the {} initializer is illegal in C. How did you manage to get that to compile?

AndreyT
Thanks, with a#define param_const_t const char (*)[MAX_LINE_LEN]it's not that ugly. It's just non-sense to me why this strictness increasing "casting" causes warning.
Visko
I've been using the {} initializer for a while. I'm using gcc, but I had no problem with visual studio's compiler. Now that you mentioned, I tried with cadul compiler (which is so ANSI standard that it wouldn't accept // as comment) and it fails. Another non-sense thing...
Visko
@Visko: AFAIK, Visual Studio won't accept it either, assuming you are compiling as C, not as C++. And your question is tagged C.
AndreyT
@Visko: I believe it's related to this entry in the C FAQ: http://c-faq.com/ansi/constmismatch.html , although it's not identical and this *particular* case should be fine.
caf
@Visko: you have to distinguish different versions of norms for C. For C99 the initializer has you gave it is no good, you'd have to write { 0 }. But // for comments is allowed, there. Best is probably to tell the compiler which norm you want to follow and then to stick to it.
Jens Gustedt
A: 

There is no good solution to this problem - I usually just comment out the const qualifier on the function parameter to show that it should ideally be const but that we also want to compile without warnings, i.e.

int getVersion(/* const */ char params[MAX_PARAM_LEN][MAX_LINE_LEN],
               const char* tagName );
Paul R