我收到此错误
已弃用从字符串常量转换为
char*
如何将字符串放入char数组中。这是我试过的:
char result[];
result = "invalid";
修改
这就是我想要做的事情
bool intToRoman (int val, char result[])
{
MAIN BODY
result = "MMM";
}
在这个函数中我正在尝试将整数转换为罗马数字。在我的主体中,我想将我的字符串(例如“MMM”)存储到我的字符数组结果中。
答案 0 :(得分:8)
您需要初始化数组:
char result[] = "invalid";
这将创建一个{8}的char
数组。
但您最好使用std::string
:
std::string result("invalid");
答案 1 :(得分:1)
如果您计划在运行时更改它,则可以使用以下任一选项:
char result[] = "invalid"; // 8 bytes in the stack
static char result[] = "invalid"; // 8 bytes in the data-section
如果您不打算在运行时更改它,则可以使用以下任一选项:
const char result[] = "invalid"; // 8 bytes in the stack
static const char result[] = "invalid"; // 8 bytes in the data-section
const char* result = "invalid"; // 8 bytes in the code-section, and a pointer (4 or 8 bytes) in the stack
static const char* result = "invalid"; // 8 bytes in the code-section, and a pointer (4 or 8 bytes) in the data-section
如果只想在运行时间稍后对其进行初始化:
char result[] = "invalid"; // 8 bytes in the stack
static char result[] = "invalid"; // 8 bytes in the data-section
...
strcpy(result,"MMM");
// But make sure that the second argument is not larger than the first argument:
// In the case above, the size of "MMM" is 4 bytes and the size of 'result' is 8 bytes