I am missing calls to free in the opendir/readdir code, and youYou should let your code be indented automatically. One of the if statements is indented too far.
I prefer the first variant, because you don't need to allocate the memory for the whole directory at once. And, as soon as the count is larger than 46, you can break out of the loop and return immediately.