【发布时间】:2016-06-22 15:30:25
【问题描述】:
const char* string_b10_e2 = {"base 10"}; //base 10
有没有办法在非常量字符串变量中读取值“base 10”? 我知道我不能使用 char*,如下所示
char * str,
str = string_b10_e2; //not allowed
因为它破坏了保持字符串不变的承诺。
但是有没有办法将值读入非常量字符串?
提前致谢。
【问题讨论】:
-
可以复制字符串。 strcpy
-
const char* string_b10_e2 = {"base 10"};不好 -
char * tempString = (char*) malloc(strlen(string_b10_e2)); strcpy(tempString,string_b10_e2);你不能用这个吗? -
C 没有字符串类型。
string_b10_e2不是字符串,而是指针!你想达到什么目标?如果你不能以一种可以理解的方式回答这个问题,请阅读 C 书籍(指针和数组部分和string.h) -
@user3443084:关于如何在 C 中复制字符串已经有很多问题了。为什么他们都没有帮助?