将字符串转换为字符数组
Converting a string to a character array
我正在收到此错误
弃用从字符串常数到
的转换char*
我将如何将字符串放入字符阵列中。这是我尝试的:
char result[];
result = "invalid";
编辑:
这就是我要做的
bool intToRoman (int val, char result[])
{
MAIN BODY
result = "MMM";
}
在此功能中,我正在尝试将整数更改为罗马数字。在我的主体中,我想将字符串(例如" MMM")存储到我的角色阵列结果中。
您需要初始化数组:
char result[] = "invalid";
这将创建一个尺寸8数组char
。
但是,使用std::string
:
std::string result("invalid");
如果您打算在运行时更改它,则可以使用以下任何选项:
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
相关文章:
- 指向指向字符数组的指针数组的指针
- 比较字符数组
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 使用无符号字符数组有效存储内存
- 错误:字符数组的初始值设定项太多
- 对字符数组中的元素执行逐位操作
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- C++ 传递二维字符数组
- 无法在 C++ 中输入字符数组
- 字符数组中的元素数
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 使用字符数组作为 Map 中的键
- C++:__aligned(__alignof__) 导致字符数组数据出现问题?
- 按顺序声明的字符数组重叠
- 在 C++ 中字符串的大小与字符数组的大小
- 寻找一种更好的方法来表示无符号字符数组
- 如何打印 2D 字符数组C++
- 从字符数组的元素中减去'a'是什么意思
- 我是否不正确地集中了这些字符数组?