Code should not mix pointer types. On systems where
sizeof(unsigned short) < sizeof(int)
, this is a program flaw.// int *children = malloc(size * sizeof(unsigned short)); int *children = malloc(size * sizeof(int)); // or even better int *children = malloc(size * sizeof *children);
For high portability, use type
size_t
orunsigned long
for valuesan array index that may exceed65,535
:// unsigned int size = count_children(input); size_t size = count_children(input); int *children = malloc(size * sizeof *children);
Code should check the return value from
fscanf(input, "%d",&count);
andfscanf(input, "%d",&children[i]);
Mixing types for finding the maximum is very questionable. Suggest re-write
size_t chck_tall(const int *children, size_t size) { size_t total = 0; int tmp_max = 0; // or maybe INT_MIN for(size_t i = size - 1; i-- > 0; ) { if(children[i] > tmp_max) { tmp_max = children[i]; total++; } } return total; }