C++ 初始化 char 和 char 指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2587349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:00:46  来源:igfitidea点击:

initializing char and char pointers

c++char

提问by ra170

What's the difference between these:

这些有什么区别:

This one works:

这个有效:

char* pEmpty = new char;
*pEmpty = 'x';

However if I try doing:

但是,如果我尝试这样做:

char* pEmpty = NULL;
*pEmpty = 'x'; // <---- doesn't work!

and:

和:

char* pEmpty = "x"; // putting in double quotes works! why??

EDIT: Thank you for all the comments: I corrected it. it was supposed to be pEmpty ='x', So, this line doesn't even compile: charpEmpty ='x'; wheras this line works: char* pEmpty ="x"; //double quotes.

编辑:感谢您的所有评论:我更正了。它应该是pEmpty ='x',所以,这一行甚至不能编译: charpEmpty ='x'; 鉴于此行有效: char* pEmpty ="x"; //双引号。

采纳答案by Jeffrey L Whitledge

The difference is that string literals are stored in a memory location that may be accessed by the program at runtime, while character literals are just values. C++ is designed so that character literals, such as the one you have in the example, may be inlined as part of the machine code and never really stored in a memory location at all.

不同之处在于字符串文字存储在程序运行时可以访问的内存位置,而字符文字只是值。C++ 的设计使得字符文字(例如您在示例中使用的文字)可以作为机器代码的一部分内联,并且根本不会真正存储在内存位置中。

To do what you seem to be trying to do, you must define a static variable of type charthat is initialized to 'x', then set pEmptyto refer to that variable.

要执行您似乎要执行的操作,您必须定义一个类型char为的静态变量,该变量初始化为'x',然后设置pEmpty为引用该变量。

回答by danben

Your second line doesn't work because you are trying to assign 'x'to pEmptyrather than *pEmpty.

您的第二行不起作用,因为您试图分配'x'pEmpty而不是*pEmpty.

Edit: Thanks to Chuck for the correction. It ALSO doesn't work because you need to allocate some memory to hold the value 'x'. See the example below.

编辑:感谢查克的更正。它也不起作用,因为您需要分配一些内存来保存 value 'x'。请参阅下面的示例。

The third line does work because you are using an initalizer rather than a regular assignment statement.

第三行确实有效,因为您使用的是初始化程序而不是常规赋值语句。

In general, you should understand how pointers and dereferencing work.

通常,您应该了解指针和取消引用的工作原理。

char *p = new char();  // Now I have a variable named p that contains 
                       // the memory address of a single piece of character
                       // data.

*p = 'x'; // Here I assign the letter 'x' to the dereferenced value of p; 
          // that is, I look up the location of the memory address contained
          // in p and put 'x' there.

p = 'x'; // This is illegal because p contains a memory address, 
         // not a character.

char q = 'x';  // Now I have a char variable named q containing the 
               // character 'x'.

p = &q;  // Now I assign the address of q (obtained with the reference
         // operator &) to p.  This is legal because p contains a memory
         // address.

回答by Chuck

You need to keep in mind what a pointer is — it's just a normal variable that holds an address, much like a charholds a character value. This address can be used to look up another variable (with the *operator).

您需要记住指针是什么——它只是一个保存地址的普通变量就像char保存字符值一样。该地址可用于查找另一个变量(使用*运算符)。

When you do char* pEmpty = new char, you're giving pEmptythe value returned by new char, which is the address ofa chunk of memory large enough to hold a char value. Then you use *pEmptyto access this memory and assign it the char value 'x'.

当你这样做时char* pEmpty = new char,你会给出pEmpty由 返回的值new char,它是大到足以容纳一个字符值的内存块的地址。然后您使用*pEmpty访问此内存并为其分配 char 值'x'

In the second example, you write pEmpty = 'x'— but remember that pEmptyis a pointer, which means it's supposed to hold an address. Is 'x'an address? No, it's a character literal! So that line isn't really meaningful.

在第二个例子中,你写pEmpty = 'x'——但记住这pEmpty是一个指针,这意味着它应该保存一个地址。是'x'地址吗?不,这是一个字符文字!所以这条线并没有真正的意义。

In the third example, you're assigning pEmptythe string literal "x". Is this an address? Yes, it is. The literal evaluates to the address of that constant string.

在第三个示例中,您正在分配pEmpty字符串文字"x"。这是地址吗?是的。文字计算为该常量字符串的地址。

Remember, pointers are a completely different thing from the type that they point to. They can be used to accessa value of that type, but they are a completely different type of their own.

请记住,指针与其指向的类型完全不同。它们可用于访问该类型的值,但它们是完全不同的类型。

回答by ZachS

The second example doesn't work for a few reasons. The first one would be that you have made the pointer point to, well, nowhere in particular. The second is that you didn't actually dereference it, so you are telling the pointer to point to the address of a character literal. As it has no address, the compiler will complain.

由于几个原因,第二个示例不起作用。第一个是你已经让指针指向,嗯,没有特别的地方。第二个是你实际上没有取消引用它,所以你告诉指针指向字符文字的地址。由于它没有地址,编译器会抱怨。

EDIT:

编辑:

Just to be clear, the asterisk (*) is the operator that dereferences pointers.

需要明确的是,星号 (*) 是取消引用指针的运算符。

回答by N 1.1

pEmpty = 'x';assigns pEmpty(rather than the memory pointed by it) the value of 'x'.

pEmpty = 'x';分配pEmpty(而不是它指向的内存)'x' 的值。

*pEmpty = 'x'; //this is probably what you want