1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
void _print_string_and_newline(const char* s) {
for(int i = 0; s[i] != '\0'; i++) {
fputc(s[i], stdout);
}
fputc('\n', stdout);
}
// Conditional jumps include: if, while, do, for, and switch
int main(int argc, char* argv[]) {
// BAD CODE follows
char s[4];
s[0] = 'a';
s[1] = 'r';
s[2] = 't';
// OOPS!!! forgot the '\0' (s[3] = '\0';)
s[3] = '\0'; // FIXED!!!
_print_string_and_newline(s);
/* Conditional jump or move depends on uninitialised value(s)
* at 0x4005C4: _print_string_and_newline (conditional_jump_3.c:7)
* by 0x400603: main (conditional_jump_3.c:22)
*/
// LESSON: The '\0' is added implicitly to string literals ("▒") in code. A string
// literal is delimited by double quotes. Unless you started with double quotes,
// the '\0' is not automatic and must be added manually.
// char s1[] = ""; // This is an array of size 1.
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2021 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.