[英]C++ - char array and the null character
I have two questions about char array. 我有两个关于char数组的问题。
from the code bellow, since arr
is const
, why doesn't the compiler give me an error since I'm rewriting it? 从下面的代码,因为
arr
是const
,为什么编译器不会给我一个错误,因为我正在重写它?
char arr[5]; // arr is a const pointer to (*)[5] array cin>>arr; //
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个空格呢?
const
, because there is no const
qualifier. const
,因为没有const
限定符。 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.
但它们不是空白。
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.
但这与你的问题无关。
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”:
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
You should not use a char type, try to use string instead char array. 你不应该使用char类型,尝试使用字符串而不是char数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.