skydoor
skydoor

Reputation: 25868

What's wrong with these pointer initialization

char * a=0;
int * b=0;
cout<<a<<a+1;
cout<<b<<b+1;

what's wrong with it

Upvotes: 0

Views: 180

Answers (3)

Pavel Radzivilovsky
Pavel Radzivilovsky

Reputation: 19114

Nothing wrong with it.

maybe something's wrong with what you expect of this code.

Upvotes: -1

UncleBens
UncleBens

Reputation: 41331

The initializations are fine. Doing pointer arithmetic on NULL pointers is wrong.

Also, cout << (const char*) assumes the operand is a valid C-style string, not a NULL pointer. If you want to print the address of the string, not the string itself, you'll need to cast it because otherwise char pointers receive special treatment.

char* a = 0;
std::cout << static_cast<void*>(a);

Upvotes: 8

newdayrising
newdayrising

Reputation: 3802

You are assigning null (0) to the value of your pointers, which means they do not reference a valid memory location.

Upvotes: 1

Related Questions