char const *variable[] - Is it me or my compiler?

Hi Folks,

I have the following dummy test code... maybe it's me that's the dummy, but hopefully not :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

void James(char const *ptr);
void James2(char const **ptr);

void James(char const *ptr)
{
	UartDebugPrintf("%s",ptr);
}

void James2(char const **ptr)
{
	UartDebugPrintf("%s", *ptr);
}

void test(void)
{
   char *test = "TEST";
   James(test);
   James2(&test);
}


The application of const to the function James()'s argument works as I expect, but it doesn't for James2(). I get the following compiler warning.

1
2
...main.c(281,1): passing argument 1 of 'James2' from incompatible pointer type
...main.c(247,1): expected 'const char **' but argument is of type 'char **'


Anyone know why I can't apply the const in the second case without a cast?

Many thanks...

void James2(char *const *ptr)

and while I'm here,
const char *test = "TEST"; too :)
Last edited on
&test is a char** not a char const**. I don't know if there is a good reason why there can't be an implicit conversion in this case.

If you change test to char const* (which it should be because it is pointing to const data) &test will become the correct type and compile as it should.
About why T** cannot be converted to T const**: http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.17
Topic archived. No new replies allowed.