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 - C Tutorial
From the course: Secure Coding in C
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
Allocating strings2m 40s
-
(Locked)
Avoiding bad string assignment1m 28s
-
(Locked)
Working with string literals2m
-
(Locked)
Minding string functions3m 47s
-
(Locked)
Storing passwords and codes1m 15s
-
(Locked)
Clearing data after use1m 13s
-
(Locked)
Challenge: The secret code40s
-
(Locked)
Solution: The secret code1m 43s
-
(Locked)
-
-