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 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
/*
* Use typecast only when
* ∙ safe… and you know why
* ∙ necessary… and you know why
*/
int _compare_tree_nodes(const void* a_lhs, const void* a_rhs) { //
// OK
// TreeNode* lhs_node = a_lhs;
// TreeNode* rhs_node = a_rhs;
// OK
// Won't compile
// return lhs_node -> frequency - rhs_node -> frequency;
return ((TreeNode*)lhs_node) -> frequency - ((TreeNode*)rhs_node) -> frequency;
// Necessary because the compiler doesn't know if ->frequency is appropriate for that addess.
// Safe because you will only call this with TreeNode* arguments.
}
void append(int value, Node** a_head, Node** a_tail) {
// …
}
int main(int argc, char* argv[]) {
// BAD
// TreeNode* new_node = (TreeNode*) malloc(sizeof(*new_node)); // unnecessary
Node* head = NULL;
Node* tail = NULL;
append(3, (Node**)head, (Node**)tail); // VERY BAD!!! Tells compiler not to warn you about your mistake.
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2020 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.