gpt4 book ai didi

C++:不推荐从字符串常量到 'LPSTR {aka char*}' [-Wwrite-strings] 的转换 - 警告。如何避免?

转载 作者:太空狗 更新时间:2023-10-29 20:41:10 26 4
gpt4 key购买 nike

在编译 C++ 代码时,我收到此警告:

deprecated conversion from string constant to 'LPSTR {aka char*}' [-Wwrite-strings]
&pi)) // Pointer to PROCESS_INFORMATION structure.
     ^

代码是:

STARTUPINFO si;
memset(&si, 0, sizeof (STARTUPINFO));
si.cb = sizeof (STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = FALSE;

PROCESS_INFORMATION pi;
memset(&pi, 0, sizeof (PROCESS_INFORMATION));

if (!CreateProcess("C:\\Program Files\\Java\\jre7\\bin\\javaw.exe",
" -jar install.jar", // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
0, // Set handle inheritance to FALSE.
CREATE_NO_WINDOW, // ON VISTA/WIN7, THIS CREATES NO WINDOW
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi)) // Pointer to PROCESS_INFORMATION structure. //Warning comes from this line - variable &pi
{
printf("CreateProcess failed\n");
return 0;
}

有没有可能做些什么来避免这种情况(我的意思是不使用 - 抑制警告)?

最佳答案

编译器告诉您 CreateProcess 的第二个参数是 char* 类型,但您传递的是 const char*。换句话说,CreateProcess 的第二个参数需要一个可修改的缓冲区,但您传递的是一个不可修改的文字。

像这样声明一个可修改的字符串:

char cmdline[] = "-jar install.jar";

关于C++:不推荐从字符串常量到 'LPSTR {aka char*}' [-Wwrite-strings] 的转换 - 警告。如何避免?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22583100/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com