C语言中将字符串存储到CHAR型数组 - 以'1.53V'为例
要将字符串'1.53V'存储在CHAR型数组temp[5]中,可以按照以下步骤进行操作:
-
确保数组temp具有足够的空间来存储字符串。在本例中,数组temp[5]足够大。
-
将字符串中的每个字符逐个存储到数组temp中。可以使用循环来遍历字符串,并将每个字符存储到数组的对应位置。
以下是一个示例代码:
#include <iostream>
#include <cstring>
int main() {
char temp[5]; // 声明一个字符数组temp[5]
const char* str = "1.53V"; // 声明一个字符串
// 将字符串中的每个字符存储到数组temp中
for (int i = 0; i < 5; i++) {
temp[i] = str[i];
}
// 输出数组temp中的内容
for (int i = 0; i < 5; i++) {
std::cout << temp[i];
}
std::cout << std::endl;
return 0;
}
输出结果为:1.53V
原文地址: https://www.cveoy.top/t/topic/bx7e 著作权归作者所有。请勿转载和采集!