Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: C++
Expertise: All
Sep 25, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Hardcoded Char Arrays

Does your C++ compiler accept the following code?

char s[3]="abc"; // illegal in C++, no place for '\0'

The standard requires that the size of a char array initialized with a literal string be sufficiently large to store a terminating null character. In other words, the declaration of s should be flagged as a compilation error because it contains only three positions instead of four. Unfortunately, the rules regarding static char arrays in C are less restrictive. Therefore, such a declaration is valid in C. Many C++ compilers still follows the old C rules and accept the declaration above when they shouldn't. Using such an array is likely to cause undefined behavior because most functions that manipulate strings expect a null-terminated array.

Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date