如何在c++中正确地赋值一个char*
How can I properly assign a char* in c++?
我的c++代码是这样的:
char* x;
switch(i){
case 0:
x = '0';
case 1:
x = "1";
...}
我不知道如何使这个工作,因为对于第一个 x = '0';
编译器抱怨:
error: invalid conversion from ‘char’ to ‘char*’
对于第二个x = "1";
,编译器报错:
warning: deprecated conversion from string constant to ‘char*’
我该怎么做?我这么做完全错了吗?
在case 0
中,您试图将x
设置为字符(类型char
),但在case 1
中,您试图将x
设置为C字符串(类型char const[2]
)。这是一种不同的报价类型;单引号用于字符,双引号用于c风格的字符串。
如果你想把它两次都设置为字符串,在x = '0'
的0
周围加上双引号。
如果你想将x
设置为一个字符,两次使用单引号并对指针解除引用,就像*x
一样,这样它就变成了*x = '0'
,或*x = '1'
, 或将x
的类型从char*
(指向字符的指针)更改为char
(字符)。那么你就不需要取消引用1
然后,如果您试图将x
设置为字符串,那么最好使用c++字符串而不是C字符串,使用std::string
。然后你可以像C字符串一样使用双引号,但是你会得到一堆额外的功能,比如自动内存管理,边界检查,以及它所拥有的所有成员函数。
1正如Nicolas Grebille指出的:在这样做之前,确保它指向一个有效的char
,或者使用new
:
char* x = new char;
或者在堆栈上创建一个char
:
char c;
char* x = &c;
重要:
如果你要在strcat
后面使用char*
(或任何需要C-string的函数),你必须正确地NULL
终止你的缓冲区。所以你需要这样做:
char x[2] = {}; // on the stack, filled with NULLs
// use a bigger number if you need more space
或
char* x = new char[2]; // on the heap, use more than 2 if you're
// storing more than 1 character
x[1] = NULL; // set the last char to NULL
如果你不这样做,如果你运气不好,你会得到垃圾,如果你运气好,你会得到段错误。
然后在上面声明x
之后,您可以执行*x = '0'
或其他任何操作。
如果您决定使用new[]
,请确保使用相应的delete[]
来释放内存
与普遍看法相反,char*
不是字符串。用std::string
代替。
#include <string>
std::string x;
switch(i){
case 0:
x = "0";
case 1:
x = "1";
...}
如果要使用char *,则需要为要存储的字符串分配空间,方法是将x的声明更改为:
char x[10]; // allocate a fixed 10-character buffer
或者在堆上动态分配空间:
x = new char[10]; // allocate a buffer that must be deleted later
则可以使用:
strcpy(x, "1"); // Copy the character '1' followed by a null terminator
// into the first two bytes of the buffer pointed to by x.
将字符串"1"复制到x所指向的缓冲区中。如果使用第二个示例,必须稍后:
delete x;
当然,如果你真的想处理字符串,有更好的方法,也有更好的方法来处理单个字符(参见其他答案)。
这是一个回答,但你可以得到字符0
, 1
,…来自int with:
char x = '0' + i;
,这将避免你的switch语句(当然是0 <= i <= 9
)。
您正在声明char
指针,未分配任何空间,然后试图为其分配一个字符。
char x;
会给你一个类型为char
的变量
char *x = new char[10];
会给你一个指针,指向一个char指针所指向的10字节的内存。
正如上面andr
首先,请找出使用std::string
的方法。如果你继续你所选择的路径,你肯定会创建有bug的程序。
已经说过了,这里是你的答案:如何声明x
,以及如何给它赋值,在很大程度上取决于你以后如何使用它。下面是一个例子:
#include <stdio.h>
int main(int ac, char **av) {
const char* x;
switch(ac) {
case 0:
x = "0";
break;
case 1:
x = "1";
break;
default:
x = "DANGER!";
break;
}
printf("%sn", x);
return 0;
}
在第一个例子中,我们将指针x
设置为指向三种可能的const char数组之一。可以稍后从这些数组中读取(就像在printf
调用中一样),但永远不能对这些数组进行写操作。
#include <stdio.h>
#include <string.h>
int main(int ac, char **av) {
char x[32]; // make sure this array is big enough!
switch(ac) {
case 0:
strcpy(x, "0");
break;
case 1:
strcpy(x, "1");
break;
default:
strcpy(x, "NOT 0 nor 1:");
break;
}
strcat(x, ", see?");
printf("%sn", x);
return 0;
}
EDIT:从第二个例子中去掉不正确的const
- 如何创建一个 char *[] 变量,以便将其传递给函数
- C Cout如何打印一个char*
- 使用C 中字符串标头的getline()函数将第一个char存储在字符串对象中
- C :在字符串中找到一个char,然后在先前的char之后找到另一个字符
- 如何从IStream读取一个char数组并将其写入另一个IStream
- 如何在c++中将计算分解为两个double和一个char
- 返回一个 char 数组而不是 const char *
- 将值从一个 char 数组存储到另一个 char 数组
- 如何在 c++ 中的另一个 char 数组中找到一个字符数组
- 如何在C 中创建一个char数组
- 当一个函数可以被赋予一个 char* 或文字字符串时,声明这个函数的正确方法是什么?
- C++使用类从多维向量输出一个char数组
- 如果您使一个char*ptr存储整数变量的地址,会发生什么
- C++将文件的所有字节都获取到一个char数组中
- 为 execvp() 创建一个 char* const *
- 将两个单词读入一个char数组元素
- 使用循环向后打印一个char数组
- 当我使用模板将一个char数组复制到另一个时出现问题
- C++创建一个char*迭代器
- 返回一个char指针(内存泄漏)