Sun Studio 12: C++ User's Guide

14.3.4 Using the char* Extractor

This predefined extractor is mentioned here because it can cause problems. Use it like this:


char x[50];
cin >> x;

This extractor skips leading whitespace and extracts characters and copies them to x until it reaches another whitespace character. It then completes the string with a terminating null (0) character. Be careful, because input can overflow the given array.

You must also be sure the pointer points to allocated storage. For example, here is a common error:


char * p; // not initialized
cin >> p;

There is no telling where the input data will be stored, and it may cause your program to abort.