简体   繁体   English

C ++ - char数组和空字符

[英]C++ - char array and the null character

I have two questions about char array. 我有两个关于char数组的问题。

  1. from the code bellow, since arr is const , why doesn't the compiler give me an error since I'm rewriting it? 从下面的代码,因为arrconst ,为什么编译器不会给我一个错误,因为我正在重写它?

     char arr[5]; // arr is a const pointer to (*)[5] array cin>>arr; // 
  2. when I initialized a char array like this: 当我初始化一个这样的char数组时:

     char arr[5]={'h','i'}; 

    if I did this: 如果我这样做:

     cout << arr << "something here \\n"; 

    it will print hisomething here . 它会hisomething here打印hisomething here I thought It should print out 我以为它应该打印出来

     hi something here 

    with 3 witespaces. 有3个witespaces。

    But if I did this: 但如果我这样做:

     for(int i = 0; i < 5; i++){ cout << arr[i]; } 

    it will printout the 3 whitespaces. 打印出3个空格。

The second case seems to prove that the compiler doesn't add any null characters. 第二种情况似乎证明编译器不添加任何空字符。 So how can the compiler ignore the 3 whitespaces? 那么编译器如何忽略3个空格呢?

  1. This array is not const , because there is no const qualifier. 这个数组不是const ,因为没有const限定符。
  2. If you don't specify remaining values in initializer list, they will be initialized to 0 . 如果未在初始化程序列表中指定剩余值,则它们将初始化为0 0 is used to terminate C strings, not as a whitespace. 0用于终止C字符串,而不是空格。

As for your claim, that for(int i=0;i<5;i++){ cout << arr[i]; } 至于你的主张,那个for(int i=0;i<5;i++){ cout << arr[i]; } for(int i=0;i<5;i++){ cout << arr[i]; } printed whitespace - how did you checked that? for(int i=0;i<5;i++){ cout << arr[i]; }打印空白-你是怎么检查的?

For me: 为了我:

#include <iostream>

int main(){
    char arr[5]={'h','i'};
    for(int i=0;i<5;i++){ std::cout << arr[i]; }
    std::cout << "X" << std::endl;
}

prints: 打印:

hiX

and hexdumped: 和hexdumped:

$ ./t | hexdump -Cv
00000000  68 69 00 00 00 58 0a                              |hi...X.|
00000007

There are '\\0' chars printed. 打印了'\\0'字符。 Their display seems to be operating system dependent. 它们的显示似乎取决于操作系统。 But they are not a whitespace. 但它们不是空白。

  1. arr itself is a "char[5]" not a "const char (*)[5]". arr本身是一个“char [5]”而不是“const char(*)[5]”。 And it is implicitly cast to a "char *" rvalue when you write cin >> arr. 当你写cin >> arr时,它会隐式地转换为“char *”rvalue。 It's not a const because it is not even a lvalue. 它不是一个常数,因为它甚至不是一个左值。

And "const char *" or "char const *" mean the lvalue pointed to cannot be changed, while "char * const" means the pointer lvalue itself cannot be change. 而“const char *”或“char const *”表示指向的左值不能改变,而“char * const”表示指针左值本身不能改变。 But this has nothing to do with you question, though. 但这与你的问题无关。

  1. First, there was no whitespace. 首先,没有空白。 And it is cin appended the null character. 它是附加空字符的cin。

An array is just an array: 数组只是一个数组:

char a[5]; //a's type is char[5];

But an array can hardly be an operand. 但是数组很难成为操作数。 Only operators I can remember that accept array type are sizeof and &(address-of) (On the other hand, this means sometimes a have to be an array. Or else if you write sizeof(a), it will give you the size of a pointer.). 只有运算符我记得接受数组类型是sizeof和&(地址)(另一方面,这意味着有时必须是一个数组。否则如果你写sizeof(a),它会给你大小一个指针。) For other operations a is converted to a char * rvalue. 对于其他操作,a转换为char * rvalue。 And yes, even when you write a[0], a[1], etc. a[0] is equivalent to *(a + 0), which operates on pointers but not arrays. 是的,即使你写一个[0],一个[1]等,a [0]相当于*(a + 0),它对指针进行操作但不对数组进行操作。

When you cannot assign to something, it doesn't always mean that thing is "const": 当你无法分配某些东西时,它并不总是意味着东西是“const”:

  1. You cannot assign to a const variable of course. 当然不能分配给const变量。
  2. You can only assign to a variable(or aka lvalue), so you cannot assign something to a rvalue(or aka value). 您只能分配给变量(或称为左值),因此您无法为rvalue(或aka值)分配内容。 So you cannot write 1 = 2, because 1 is an rvalue not a variable, not because 1 is "const". 所以你不能写1 = 2,因为1是rvalue而不是变量,不是因为1是“const”。
  3. You must assign something to a variable that matches its type. 您必须将某些内容分配给与其类型匹配的变量。 So if you have a const char *p and a char *q, you cannot write q = p. 所以如果你有一个const char * p和一个char * q,你就不能写q = p。 Their types don't match. 他们的类型不匹配。 And again, it doesn't mean q is const, for it's obviously not. 而且,它并不意味着q是常数,因为它显然不是。 But you can write p = q, because char * can be implicitly cast to const char *. 但是你可以编写p = q,因为char *可以隐式地转换为const char *。 But const char * have to be cast to char * explicitly. 但是const char *必须明确地转换为char *。

arr acts a const pointer which means that you cannot set the pointer to another address. arr作为const指针,这意味着您不能将指针设置为另一个地址。 It is not a pointer to a const so you can change the data at the address the pointer points to. 它不是指向const的指针,因此您可以更改指针指向的地址处的数据。

Example: 例:

arr=&another_variable; //Illegal
*arr='A';              //Legal
  1. the first it is not const. 第一个不是常数。 the second, this code is not safe. 第二,这段代码不安全。
  2. u should to setup termination symbol '\\0' in the end. 你应该在最后设置终止符号'\\ 0'。

You should not use a char type, try to use string instead char array. 你不应该使用char类型,尝试使用字符串而不是char数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM