Need help with struct *** dirent ???

Discussion in 'C Programming' started by eddie, Jan 10, 2009.

  1. eddie

    eddie Guest

    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
    eddie, Jan 10, 2009
    #1
    1. Advertising

  2. eddie

    Flash Gordon Guest

    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
    Flash Gordon, Jan 11, 2009
    #2
    1. Advertising

  3. In article <>,
    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
    >


    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);
    Kenny McCormack, Jan 11, 2009
    #3
  4. eddie

    eddie Guest

    On Jan 10, 9:03 pm, "Anthony Fremont" <> 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.
    eddie, Jan 11, 2009
    #4
  5. eddie

    Richard Guest

    Flash Gordon <> 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.org/man_pages/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
    Richard, Jan 11, 2009
    #5
  6. eddie

    Guest

    On Jan 11, 12:16 am, (Kenny McCormack)
    wrote:
    > In article <>,
    > 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.


    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
    , Jan 11, 2009
    #6
  7. In article <>,
    Han from China - Master Troll <> 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.
    Kenny McCormack, Jan 11, 2009
    #7
  8. eddie

    Richard Guest

    Han from China - Master Troll <> 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
    Richard, Jan 12, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Pohlack

    sizeof(struct dirent.d_ino)

    Martin Pohlack, Sep 8, 2003, in forum: C Programming
    Replies:
    19
    Views:
    1,085
    Keith Thompson
    Sep 10, 2003
  2. **--> That Guy Downstairs
    Replies:
    14
    Views:
    738
    S7Solutions
    Feb 21, 2006
  3. Dirent.h

    , Feb 26, 2006, in forum: C++
    Replies:
    3
    Views:
    727
    Larry I Smith
    Feb 26, 2006
  4. Replies:
    9
    Views:
    520
    P.J. Plauger
    May 10, 2006
  5. Sascha Wüstemann
    Replies:
    4
    Views:
    1,055
    Ike Naar
    Aug 3, 2011
Loading...

Share This Page