From the course: Secure Coding in C

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Avoiding bad string assignment

Avoiding bad string assignment - C Tutorial

From the course: Secure Coding in C

Start my 1-month free trial

Avoiding bad string assignment

- [Instructor] Strings sport an invisible character, the null character, which the C language uses to flag the end of the string. The string literal is a sign at line five in this code, and at first glance it looks okay. The string contains three characters, and the array size is set to three. The problem is that the compiler automatically creates and it pins the null character when you code a string literal like this. So the array is undersized, which is unsafe. Here's the fix, which is to remove the element count, when declaring the string literal. The compiler automatically supplies the value, which is four, that's less work for you. In this code, you see a legitimate declaration of a character array with three elements at line five. It's not a string, no it's a character array, initialized to three character values, A, B, and C. If you treat this like an array, the code is cool, but in line seven, it's treated like a…

Contents