From: Boyd, T. M. <tm...@cc...> - 2008-06-05 22:08:35
|
> -----Original Message----- > From: min...@li... [mailto:mingw-users- > bo...@li...] On Behalf Of Tuomo Latto > Sent: Thursday, June 05, 2008 4:49 PM > To: MinGW Users List > Subject: Re: [Mingw-users] Linux dirent->d_type ... Windows equivalent? > [OT] > > Boyd, Todd M. wrote: > > [code] > > struct dirent ** namelist; > [...] > > qsort(namelist, n, sizeof(struct dirent), alphaDirSort); > ^^^^^^^^^^^^^^^^^^^^^ > > It's an array of pointers, not an array of structs. > > Also, your debug prints might have buffering issues. > I prefer fprintf() with stderr and strings ending with a newline. > Using fflush() won't hurt either, except performance-wise. You're right about the buffering issues. I've been using fprintf(stderr, x, x) now, and the "Inside alphaDirSort" was displayed. Using this debugging mechanic, I've discovered that it's how I'm dereferencing the dirent pointers in alphaDirSort. I've changed it from: alphaDirSort(const void *, const void *); to: alphaDirSort(const struct dirent *, const struct dirent *); In order to try and ease some of my confusion, but it didn't really do anything for me except spare me from casting them later. I have tried several ways trying to access the d_name member. All of them return garbage rather than the d_name (or are invalid operations). Given struct dirent * a: puts(a->d_name); // garbage puts(a.d_name); // err: not a structure or union puts((*a).d_name); // garbage puts((*a)->d_name); // err: invalid type argument of -> puts((&a).d_name); // err: not a structure or union puts((&a)->d_name); // err: not a structure or union Changing the namelist malloc line to reflect pointers to dirents instead of dirents caused an error and munmap_chunk() to be run. alphaDirSort() returned the same garbage, regardless of if qsort() was told to handle pointers or structs. I think I need to do a bit more research on qsort(). Since I am passing it a valid array that can be accessed in a "normal" way (using [] index specifiers), there should be some code out there that will explain to me the proper method for accessing the values passed through qsort() to whatever comparison function it will use. I'll do research on my own, as I've obviously missed some critical information about qsort(), and I'm out of practice dealing with pointers. Thanks anyway (and sorry for clogging the listserv), Todd Boyd Web Programmer |