如何在c++中正确地赋值一个char*

How can I properly assign a char* in c++?

本文关键字:一个 char 赋值 c++ 正确地      更新时间:2023-10-16

我的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 Caron所指出的,因为你使用的是c++,所以你真的应该使用一个实际的字符串来让你的工作更轻松

首先,请找出使用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