Need help with struct *** dirent ???

E

eddie

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
 
F

Flash Gordon

eddie said:
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.
 
K

Kenny McCormack

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);
 
E

eddie

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.
 
R

Richard

Flash Gordon said:
As you have provided the prototype the source of the function is irrelevant.


* 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
 
G

Guest

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);
 
K

Kenny McCormack

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.
 
R

Richard

Han from China - Master Troll said:
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.

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,005
Messages
2,570,264
Members
46,859
Latest member
HeidiAtkin

Latest Threads

Top