Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 编译器警告字符串转换-是因为双引号吗?_C++ - Fatal编程技术网

C++ 编译器警告字符串转换-是因为双引号吗?

C++ 编译器警告字符串转换-是因为双引号吗?,c++,C++,如果我输入5,我希望它显示May 但他们总是在警告,我怎样才能让它起作用 #include <iostream> using namespace std; int main() { int month; char *m_name[] = {" ", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

如果我输入5,我希望它显示May 但他们总是在警告,我怎样才能让它起作用

#include <iostream>

using namespace std;

int main() {
    int month;
    char *m_name[] = {"   ", "Jan", "Feb", "Mar", "Apr", "May",
                      "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
    cout << "Enter month: ";
    cin >> month;
    if (month >= 1 && month <= 12)
        cout << m_name[month];
    else cout << "Illegal month";
    return 0;
}

该警告可能是由于字符*。将其更改为char const*


说明:字符串文字的类型为char const[N],其中N是字符串中的字符数+1,表示末尾的NUL。通常,这种类型的数组只能转换为char const*,但有一个特殊的规则允许将字符串文本转换为char*,以实现C兼容性。这是一种不安全的转换,因此编译器会发出警告。

请不要发布代码的图像,而是复制代码并将其粘贴到问题中。使用,您的代码将具有语法突出显示,我们也可以复制代码,以便自己测试它。另外,请告诉我们您从程序中获得的确切输出,以及您希望它具有的输出。请回答标题,不要回答其他任何问题:不,双引号适用于多个字符。单引号用于单个字符。您的代码看起来不错。你确定这是你编译的吗?警告是什么?它在没有任何警告的情况下对我有效。
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
                       "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
                                                                      ^
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]