Pointers Are Dangerous

Remember that the compiler doesn't care if you try to get or put the value of an element that is outside an array's size. This is because arrays are really convenient shorthand for pointers.

Pointers are just as dangerous as arrays for the same reasons. Have a look at this:

char *SomePointer;
*SomePointer = 'x'; // Oops.

What do you think this does? If you answer, “I don't know,” you're right. The behavior of a program with an uninitialized pointer is undefined and unpredictable.

char *SomePointer = NULL;
*SomePointer = 'x'; // Oops.

In this case, NULL means “points to nothing.” If you are lucky, the program stops. If you are unlucky, your entire system may freeze or crash. Still, NULL is often used to indicate that ...

Get SAMS Teach Yourself C++ in 10 Minutes SECOND EDITION now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.