in the snippet code bellow when I pass a literal string to the function it gives me a warning ISO C++ forbids converting a string constant to 'char*
but when I assign a character array to this literal the warning will be gone. I know that the type of string literals in C++ is constant character array but the type of ch
variable is just char
.(not constant char
)
#include <iostream>
using namespace std;
void func(char s[])
{
cout << s;
}
int main() {
char ch[] = "what";
func(ch);
func("what"); //gives warning
return 0;
}
and I have one question more. when I add const
to input parameter type of func
function there is no warning in this situation too even though I pass a character array to the function not const character array.I thought it should cause a warning for fucn(ch)
call because ch
is a character array not constant character array.
#include <iostream>
using namespace std;
void func(const char s[])
{
cout << s;
}
int main() {
char ch[] = "what";
func(ch);
func("what");
return 0;
}