From the course: C: Data Structures, Pointers, and File Systems

Unlock the full course today

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

Avoiding string problems

Avoiding string problems - C Tutorial

From the course: C: Data Structures, Pointers, and File Systems

Start my 1-month free trial

Avoiding string problems

- [Narrator] As a C programmer you must be extremely careful when dealing with strings as the compiler doesn't check that the storage is the proper size or that the null character is present. Unlike other higher level languages the burden is on you, the programmer, to check these conditions. Fortunately I have only a few suggestions plus some other tips on working with strings to help avoid problems. First, ensure that proper storage is allocated for a string. For array notion properly set the buffer size. That's the number of characters you plan on putting into the string plus one for the null character, and if you declare a string literal you can just leave the brackets empty. The compiler calculates the proper storage required. If you're using a pointer to allocate string storage ensure that the buffer size is large enough to hold the string plus one for the null character. This type of allocation makes more sense if you've studied pointers and memory allocation. The number of…

Contents