Kam*_*ski -1 c++ stack char bison
我一直在尝试打印一个字符串文字,但似乎我做错了,因为我收到了编译警告.这可能是由于错误的格式化或我对c_str()
功能的误解,我认为应该返回一个string
.
parser.y: In function ‘void setVal(int)’:
parser.y:617:41: warning: format not a string literal and no format arguments [-Wformat-security]
Run Code Online (Sandbox Code Playgroud)
617行:
sprintf(temp, constStack.top().c_str());
Run Code Online (Sandbox Code Playgroud)
有这些声明
#include <stack>
const int LENGTH = 15;
char *temp = new char[LENGTH];
stack<string> constStack;
Run Code Online (Sandbox Code Playgroud)
如何为字符串提供正确的格式?
Ala*_*kes 10
简单 - 提供格式字符串:
sprintf(temp, "%s", constStack.top().c_str());
Run Code Online (Sandbox Code Playgroud)
但更好,更好:
string temp = constStack.top();
Run Code Online (Sandbox Code Playgroud)