tags:

views:

163

answers:

6

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: char pEmpty ='x'; wheras this line works: char* pEmpty ="x"; //double quotes.

+3  A: 

Your second line doesn't work because you are trying to assign 'x' to pEmpty rather than *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.

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.
danben
The second one would probably crash if he'd tried to assign to `*pEmpty` — dereferencing NULL is not valid in C++. And your second example is undefined behavior too since you try to dereference an uninitialized pointer.
Chuck
yes, I corrected it. it was supposed to be *pEmpty ='x',So, this line doesn't even compile: char* pEmpty ='x';wheras this line works: char* pEmpty ="x"; //double quotes.
ra170
A: 

Single quotes and no * in the declaration indicate a single character.

Double quotes and a * in the declaration indicate a string of zero or more characters. A * after the declaration dereferences the pointer, which in this case retrieves the first char of the string.

You second example doesn't work because the variable is declared with a star, then used with single quotes despite no dereference.

Potatoswatter
Explain downvote? Simple answer for a simple question. He isn't performing any memory allocation or leaving pointers uninitialized.
Potatoswatter
A: 

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.

ZachS
He didn't tell it to point to the address of the literal, he told it to convert the character literal's value into an address.
Chuck
A: 

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

*pEmpty = 'x'; //this is probably what you want
N 1.1
A: 

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.

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

Jeffrey L Whitledge
You mean like this? static char xx = 'x';char* pEmpty = It works, awesome!, I just wanted to learn something..and it seemed peculiar that 'x' wouldn't work and "x" would work. Now I understand that it's a character literal vs. string literalThanks!
ra170
A: 

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

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

In the second example, you write pEmpty = 'x' — but remember that pEmpty is 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.

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

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

Chuck