简体   繁体   English

关于c ++中char指针的混淆

[英]confusion about char pointer in c++

I'm new in c++ language and I am trying to understand the pointers concept. 我是c ++语言的新手,我正在尝试理解指针概念。

I have a basic question regarding the char pointer, 我有关于char指针的基本问题,

What I know is that the pointer is a variable that stores an address value, so when I write sth like this: 我所知道的是指针是一个存储地址值的变量,所以当我写这样的时候:

char * ptr = "hello";

From my basic knowledge, I think that after = there should be an address to be assigned to the pointer, but here we assign "hello" which is set of chars. 根据我的基本知识,我认为在=之后应该有一个地址分配给指针,但是在这里我们分配“hello”这是一组chars。 So what does that mean ? 那是什么意思呢?
Is the pointer ptr points to an address that stores "hello"? 指针ptr是否指向存储“hello”的地址? or does it store the hello itself? 或者它本身存储你好?
Im so confused, hope you guys can help me.. 我很困惑,希望你们能帮帮我..

Thanks in advance. 提前致谢。

ptr holds the address to where the literal "hello" is stored at. ptr将地址保存到文本"hello"的存储位置。 In this case, it points to a string literal. 在这种情况下,它指向一个字符串文字。 It's an immutable array of characters located in static (most commonly read-only) memory. 它是位于静态(最常见的只读)内存中的不可变字符数组。

You can make ptr point to something else by re-assigning it, but before you do, modifying the contents is illegal. 您可以通过重新分配来使ptr指向其他内容,但在此之前,修改内容是非法的。 (its type is actually const char* , the conversion to char* is deprecated (and even illegal in C++11) for C compatibility. (它的类型实际上是const char* ,为了C兼容性,不推荐转换为char* (在C ++ 11中甚至是非法的)。

Because of this guarantee, the compiler is free to optimize for space, so 由于这种保证,编译器可以自由地优化空间,因此

char * ptr = "hello";
char * ptr1 = "hello";

might yield two equal pointers. 可能会产生两个相等的指针。 (ie ptr == ptr1 ) (即ptr == ptr1

The pointer is pointing to the address where "hello" is stored. 指针指向存储“hello”的地址。 More precisely it is pointing the 'h' in the "hello". 更确切地说,它将“h”指向“你好”。

"hello" is a string literal : a static array of characters. "hello"是一个字符串文字 :一个静态的字符数组。 Like all arrays, it can be converted to a pointer to its first element, if it's used in a context that requires a pointer. 像所有数组一样,如果它在需要指针的上下文中使用,它可以转换为指向其第一个元素的指针。

However, the array is constant, so assigning it to char* (rather than const char* ) is a very bad idea. 但是,数组是常量,所以将它赋给char* (而不是const char* )是一个非常糟糕的主意。 You'll get undefined behaviour (typically an access violation) if you try to use that pointer to modify the string. 如果您尝试使用该指针修改字符串,您将获得未定义的行为(通常是访问冲突)。

编译器将“找到某个地方”,它可以将字符串"hello" ,而ptr将具有“某处”的地址。

When you create a new char* by assigning it a string literal, what happens is char* gets assigned the address of the literal. 当您通过为其指定字符串文字来创建新的char *时,会发生的事情是为char *分配文字的地址。 So the actual value of char* might be 0x87F2F1A6 (some hex-address value). 所以char *的实际值可能是0x87F2F1A6(一些十六进制地址值)。 The char* points to the start (in this case the first char) of the string. char *指向字符串的开头(在本例中为第一个char)。 In C and C++, all strings are terminated with a /0, this is how the system knows it has reached the end of the String. 在C和C ++中,所有字符串都以/ 0结尾,这就是系统知道它到达字符串末尾的方式。

char* text = "Hello!" can be thought of as the following: 可以被认为如下:

At program start, you create an array of chars, 7 in length: {'H','e','l','l','o','!','\\0'} . 在程序启动时,您将创建一个长度为7的字符数组: {'H','e','l','l','o','!','\\0'} The last one is the null character and shows that there aren't any more characters after it. 最后一个是空字符,表示后面没有任何字符。 [It's more efficient than keeping a count associated with the string... A count would take up perhaps 4 bytes for a 32-bit integer, while the null character is just a single byte, or two bytes if you're using Unicode strings. [它比保持与字符串关联的计数更有效...对于32位整数,计数可能占用大约4个字节,而空字符只占一个字节,如果使用Unicode字符串则占两个字节。 Plus it's less confusing to have a single array ending in the null character than to have to manage an array of characters and a counting variable at the same time.] 另外,让单个数组以空字符结尾比不必同时管理字符数组和计数变量更容易让人困惑。

The difference between creating an array and making a string constant is that an array is editable and a string constant (or 'string literal') is not. 创建数组和使字符串不变的区别在于数组是可编辑的,而字符串常量(或“字符串文字”)则不是。 Trying to set a value in a string literal causes problems: they are read-only. 尝试在字符串文字中设置值会导致问题:它们是只读的。

Then, whenever you call the statement char* text = "Hello!" 然后,无论何时调用语句char* text = "Hello!" , you take the address of that initial array and stick it into the variable text . ,您获取该初始数组的地址并将其粘贴到变量text Note that if you have something like this... 请注意,如果你有这样的东西......

char* text1 = "Hello!";
char* text2 = "Hello!";
char* text3 = "Hello!";

...then it's quite possible that you're creating three separate arrays of {'H','e','l','l','o','!','\\0'} , so it would be more efficient to do this... ...那么你很可能会创建三个独立的{'H','e','l','l','o','!','\\0'}数组,所以它会这样做会更有效率......

char* _text = "Hello!";
char* text1 = _text;
char* text2 = _text;
char* text3 = _text;

Most compilers are smart enough to only initialize one string constant automatically, but some will only do that if you manually turn on certain optimization features. 大多数编译器足够聪明,只能自动初始化一个字符串常量,但有些只会在您手动打开某些优化功能时执行此操作。

Another note: from my experience, using delete [] on a pointer to a string literal doesn't cause issues, but it's unnecessary since as far as I know it doesn't actually delete it. 另一个注意事项:从我的经验来看,在指向字符串文字的指针上使用delete []不会导致问题,但这是不必要的,因为据我所知它实际上并没有删除它。

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

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