I am getting compilation error. Compiler says ambiguous function call. When const qualifier is removed from below line then it compiled successfully and work as expected.
It works if you use the same parameter type (const char*) for both functions. String literals give you a pointer to const data so you shouldn't really be using pointers to non-const in main anyway.
Then why do you even need to ask? You already have found a solution for yourself.
It's always a good idea to understand why something works. Otherwise there's a good chance that the "fix" works by accident and will break if you even breath on the code.
I hope that someone else will give a definitive answer. The error is at line 21. I may be wrong, but I seem to recall that the compiler is allowed to make only 1 implicit conversion to resolve a function call. So in this case, it can convert b to a const and call fun(char *) const or it can convert ptr1 to a const and call fun(constchar*). Since it has to do one or the other, the call is ambiguous.
> in this case, it can convert b to a const and call fun(char *) const
> or it can convert ptr1 to a const and call fun(const char*).
> Since it has to do one or the other, the call is ambiguous.
Yes. Neither one is a "better viable function" than the other.