Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Need help with struct *** dirent ???

Reply
Thread Tools

Need help with struct *** dirent ???

 
 
eddie
Guest
Posts: n/a
 
      01-10-2009


Hello: I'm at a complete loss trying to understand the stuct
dirent*** namelist notation in the manpage of scandir(3) for BSD

int scandir(const char *dirname, struct dirent ***namelist, int
(*select)(struct dirent *),int (*compar)(const void *, const void *));

I've never seen this before. Tried to google it but no luck. The
only thing it reminds me of is a pointer to a pointer.

Any help? Thanks

 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      01-11-2009
eddie wrote:
>
> Hello: I'm at a complete loss trying to understand the stuct
> dirent*** namelist notation in the manpage of scandir(3) for BSD


As you have provided the prototype the source of the function is irrelevant.

> int scandir(const char *dirname, struct dirent ***namelist, int
> (*select)(struct dirent *),int (*compar)(const void *, const void *));
>
> I've never seen this before. Tried to google it but no luck. The
> only thing it reminds me of is a pointer to a pointer.


* is a pointer to something
** is a pointer to a pointer to something
*** is a pointer to a pointer to a pointer to something
**** is a pointer to a pointer to a pointer to a pointer to something
....

In this case, the "something" being pointed to is a struct dirent.

It really is that simple. Each * is adding another "is a pointer to" to
the start of the description. So having understood pointers and pointers
to pointers, the rest is just generalising and re-applying what you
already know.
--
Flash Gordon
 
Reply With Quote
 
 
 
 
Kenny McCormack
Guest
Posts: n/a
 
      01-11-2009
In article <(E-Mail Removed)>,
eddie <(E-Mail Removed)> wrote:
>
>
>Hello: I'm at a complete loss trying to understand the stuct
>dirent*** namelist notation in the manpage of scandir(3) for BSD
>
>int scandir(const char *dirname, struct dirent ***namelist, int
>(*select)(struct dirent *),int (*compar)(const void *, const void *));
>
>I've never seen this before. Tried to google it but no luck. The
>only thing it reminds me of is a pointer to a pointer.
>
>Any help? Thanks
>


I'll take a shot at it (although I'm sure the pedants will find ways to
find fault with my explanations).

Basically, whenever you see *(anything) in the formal parameter list of
a function, you should think of it as "returning an (anything)".
Further, the passed parameter will usually be of the form: &something
with the result that the returned (anything) is placed in (something).
Note: There are exceptions in the cases of arrays and strings, but
ignore that for now.

So, the example in the man page for scandir shows us declaring:

struct dirent **namelist;

and passing &namelist to scandir(). You can think of the above
declaration as declaring an array of pointers to dirents (yes, pedants
will have something to say here). So, when you call scandir with
&namelist, you get back in namelist an array of pointers to dirents.

Then you walk through that array as shown in the example:

while (n--) {
printf("%s\n", namelist[n]->d_name);
free(namelist[n]);
}
free(namelist);

 
Reply With Quote
 
eddie
Guest
Posts: n/a
 
      01-11-2009
On Jan 10, 9:03*pm, "Anthony Fremont" <(E-Mail Removed)> wrote:
> eddie wrote:
> > Hello: *I'm at a complete loss trying to understand the stuct
> > dirent*** namelist notation in the manpage of scandir(3) for BSD

>
> > int scandir(const char *dirname, struct dirent ***namelist, int
> > (*select)(struct dirent *),int (*compar)(const void *, const void *));

>
> > I've never seen this before. *Tried to google it but no luck. *The
> > only thing it reminds me of is a pointer to a pointer.

>
> > Any help? *Thanks

>
> scandir wants to pass you back an array of pointers to dirent structures.
> In order to allow that, you pass scandir the address of the namelist pointer
> you set up something like this:
> struct dirent **namelist;
> Your call to scandir would look something like this:
> i=scandir("/", &namelist, selectroutine, compareroutine);


Thanks a lot to Everyone!!! You guys have been a great help!!! The
headache is finally going away.
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      01-11-2009
Flash Gordon <(E-Mail Removed)> writes:

> eddie wrote:
>>
>> Hello: I'm at a complete loss trying to understand the stuct
>> dirent*** namelist notation in the manpage of scandir(3) for BSD

>
> As you have provided the prototype the source of the function is irrelevant.
>
>> int scandir(const char *dirname, struct dirent ***namelist, int
>> (*select)(struct dirent *),int (*compar)(const void *, const void *));
>>
>> I've never seen this before. Tried to google it but no luck. The
>> only thing it reminds me of is a pointer to a pointer.

>
> * is a pointer to something
> ** is a pointer to a pointer to something
> *** is a pointer to a pointer to a pointer to something
> **** is a pointer to a pointer to a pointer to a pointer to something


Google up the cdecl tool.

,----
| >explain int ***nameList
| > declare nameList as pointer to pointer to pointer to int
`----

http://gd.tuwien.ac.at/linuxcommand....es/cdecl1.html

--
I'm not a person who particularly had heros when growing up.
- Dennis Ritchie when asked about the hero worship coming from c.l.c
 
Reply With Quote
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      01-11-2009
On Jan 11, 12:16*am, (E-Mail Removed) (Kenny McCormack)
wrote:
> In article <(E-Mail Removed)>,
> eddie *<(E-Mail Removed)> wrote:


> >Hello: *I'm at a complete loss trying to understand the stuct
> >dirent*** namelist notation in the manpage of scandir(3) for BSD

>
> >int scandir(const char *dirname, struct dirent ***namelist, int
> >(*select)(struct dirent *),int (*compar)(const void *, const void *));

>
> >I've never seen this before. *Tried to google it but no luck. *The
> >only thing it reminds me of is a pointer to a pointer.


a-pointer-to-pointer-to-pointer-to-struct-dirent

> I'll take a shot at it (although I'm sure the pedants will find ways to
> find fault with my explanations).
>
> Basically, whenever you see *(anything) in the formal parameter list of
> a function, you should think of it as "returning an (anything)".


um. no.

consider strlen (const char*)

this doesn't return anything via the parameter.
I suggest the OP looks at the other replies.

> Further, the passed parameter will usually be of the form: &something


strlen ("hello");

> with the result that the returned (anything) is placed in (something).
> Note: There are exceptions in the cases of arrays and strings, but
> ignore that for now.


this is a very large exception to just ignore

> So, the example in the man page for scandir shows us declaring:
>
> * * struct dirent **namelist;
>
> and passing &namelist to scandir(). *You can think of the above
> declaration as declaring an array of pointers to dirents (yes, pedants
> will have something to say here).


not me though

> *So, when you call scandir with
> &namelist, you get back in namelist an array of pointers to dirents.


yes. But this doesn't cover all cases

> Then you walk through that array as shown in the example:
>
> while (n--) {
> * * printf("%s\n", namelist[n]->d_name);
> * * free(namelist[n]);
> * * }
> free(namelist);


--
Nick Keighley

 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      01-11-2009
In article <(E-Mail Removed) t>,
Han from China - Master Troll <(E-Mail Removed)> wrote:
>Nick Keighley wrote:
>> Kenny McKormack wrote:
>>> Further, the passed parameter will usually be of the form: &something

>>
>> strlen ("hello");
>>
>>> with the result that the returned (anything) is placed in (something).
>>> Note: There are exceptions in the cases of arrays and strings, but
>>> ignore that for now.

>>
>> this is a very large exception to just ignore

>
>Are you still reading Usenet posts in a one-pass manner, Keighley?


Sure seems that way.

Or, to put it another way, I think the real problem is that he is using
a very poor editor that doesn't have the ability to go back and change
text before the cursor. Once it is written, it is history (as they say).

It is, in fact, amazing how crippled most so-called programmers are by
their lack of a good editor.

 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      01-12-2009
Han from China - Master Troll <(E-Mail Removed)> writes:

> Nick Keighley wrote:
>> Kenny McKormack wrote:
>>> Further, the passed parameter will usually be of the form: &something

>>
>> strlen ("hello");
>>
>>> with the result that the returned (anything) is placed in (something).
>>> Note: There are exceptions in the cases of arrays and strings, but
>>> ignore that for now.

>>
>> this is a very large exception to just ignore

>
> Are you still reading Usenet posts in a one-pass manner, Keighley?



*lol* : Pure Genius. But one needs to have read c.l.c for a while to
appreciate that wonderful, accurate, cutting and oh so true line.


>
>>> So, when you call scandir with
>>> &namelist, you get back in namelist an array of pointers to dirents.

>>
>> yes. But this doesn't cover all cases

>
> What do you mean by "all cases"? The error case? The use of other
> scandir(3) parameters? Since the OP has seen the prototype and has
> read the man page, wouldn't it be smart to deduce that he doesn't
> need such things pointed out in full CLC autistic manner?


Amazing isn't it?

>
> Yours,
> Han from China
>
> --
> "Only entropy comes easy." -- Anton Chekhov
>


--
I'm not a person who particularly had heros when growing up.
- Dennis Ritchie when asked about the hero worship coming from c.l.c
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
scandir file_select, text operations on const struct dirent *namelist) Sascha W├╝stemann C Programming 4 08-03-2011 07:27 AM
Getting MSVC to compile proper unix stuff, like hash_map, fts.h, dirent.h collection60@hotmail.com C++ 9 05-10-2006 06:59 PM
Dirent.h sargonisaac@gmail.com C++ 3 02-26-2006 05:24 PM
Q: portable way copy files using C on Unix an Windows -- dirent or similar **--> That Guy Downstairs C Programming 14 02-21-2006 12:37 PM
sizeof(struct dirent.d_ino) Martin Pohlack C Programming 19 09-09-2003 11:51 PM



Advertisments