Initializing char and char pointers

What is the difference between the two:

It works:

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

However, if I try to do:

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

and

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

EDIT: Thanks for all the comments: I fixed this. it should have been pEmpty = 'x', So this line doesn't even compile: char pEmpty = 'x'; if this line works: char * pEmpty = "x"; // double quotes.

+4
source share
5 answers

The difference is that string literals are stored in a memory location that the program can access at run time, while character literals are just values. C ++ is designed in such a way that character literals, such as the one you have in the example, can be embedded as part of the machine code and are never stored in memory at all.

To do what you are trying to do, you must define a static variable of type char that initializes to 'x' , and then set pEmpty to refer to this variable.

0
source

The second line does not work, because you are trying to assign 'x' pEmpty , not *pEmpty .

Edit : thanks to Chuck for fixing. It also does not work, because you need to allocate some memory to store the value of 'x' . See the example below.

The third line works because you are using an intializer, not a regular assignment operator.

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. 
+9
source

You need to keep in mind that such a pointer is just a normal variable that contains the address , just as char contains the value of a character. This address can be used to search for another variable (with the * operator).

When you do char* pEmpty = new char , you give pEmpty value returned by new char , which is the address of a piece of memory large enough to hold the char value. Then you use *pEmpty to access this memory and set it to char 'x' .

In the second example, you write pEmpty = 'x' - but remember that pEmpty is a pointer , which means that it must contain an address . Address 'x' ? No, this is a literal symbol! So this line does not make sense.

In the third example, you assign pEmpty string literal "x" . This adress? Yes. A literal evaluates the address of this constant string.

Remember that pointers are a completely different thing from the type they are pointing to. They can be used to access a value of this type, but they are of a completely different type.

+2
source

The second example does not work for several reasons. The first one is that you pointed to a pointer point, well, nowhere else in particular. Secondly, you did not actually dereference it, so the pointer pointed to the address of the character literal. Since it has no address, the compiler will complain.

EDIT:

To be clear, an asterisk (*) is an explode pointer operator.

0
source

pEmpty = 'x'; sets pEmpty (and not the memory it specifies) to 'x'.

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

Source: https://habr.com/ru/post/1306160/


All Articles