Solutions to selected problems for
the Exam 2 Review
notes.h
queue.h
- Answers for the
strcat and strcpy problems:
strcpy(s, "abc"); printf("%s", s); -
prints abc (nothing tricky about this one)
strcpy(s, "crush"); printf("%s", s); -
undefined; s has just 5 characters allocated, so it is not
clear what would happen with the '\0' at the end of
"crush". If it overwrites some critical piece of data, the program could
crash.
strcpy(s, "abc"); strcpy(&s[1], "def"); printf("%s",
s); - prints adef since this copies def on top of
bc.
strcpy(s, "abc"); strcat(s, "x"); printf("%s", s); -
prints abcx (nothing tricky)
strcpy(s, "ab"); strcat(s, s); printf("%s", s); -
undefined; since s is being extended while copying itself
to its "tail", it is possible that this operation will never end until
the processor reaches the end of memory.
strcpy(s, "abc"); printf("%d", strcmp("abc", s)); -
prints 0 since they are the same
strcpy(s, "abc"); printf("%d", strcmp("def", s)); -
partially undefined; it could be 1 since 'd' is after 'a', but it could
also be any other positive integer.