Return of struct

J

JR

I need some help. I am trying to return a dirent struct location so i
can access what the function found in main(). I dont understand
pointers very well and think that is were i am getting it wrong. If i
print everything from getdirlist everything is fine. I just cant pass
along the namelist scandir creates. Any help would be great.

JR



#include <dirent.h>


struct dirent getdirlist(char *dirstart)
{
struct dirent **namelist;
int n;

n = scandir(dirstart, &namelist, 0, alphasort);
if (n < 0) perror("scandir");
return **namelist;
}

int main()

{

struct dirent *dirlist;

int c=0;
char currentdir;
currentdir='.';
dirlist = getdirlist("/root");
while(c<sizeof(dirlist))

{

printf("%s\n",dirlist[c]->d_name);
c=c+1;

}
}
 
S

SM Ryan

#
# I need some help. I am trying to return a dirent struct location so i
# can access what the function found in main(). I dont understand
# pointers very well and think that is were i am getting it wrong. If i
# print everything from getdirlist everything is fine. I just cant pass
# along the namelist scandir creates. Any help would be great.
#
# JR
#
#
#
# #include <dirent.h>
#
#
# struct dirent getdirlist(char *dirstart)
# {
# struct dirent **namelist;
# int n;
#
# n = scandir(dirstart, &namelist, 0, alphasort);
# if (n < 0) perror("scandir");
# return **namelist;
# }
#
# int main()
#
# {
#
# struct dirent *dirlist;
#
# int c=0;
# char currentdir;
# currentdir='.';
# dirlist = getdirlist("/root");

getdirlist is (struct dirent) and dirlist is (struct dirent*).
You can't assign a struct to pointer and expect it to make
sense.


# while(c<sizeof(dirlist))
#
# {
#
# printf("%s\n",dirlist[c]->d_name);
# c=c+1;
#
# }
# }
#
#
#
 
A

Artie Gold

JR said:
I need some help. I am trying to return a dirent struct location so i
can access what the function found in main(). I dont understand
pointers very well and think that is were i am getting it wrong. If i
print everything from getdirlist everything is fine. I just cant pass
along the namelist scandir creates. Any help would be great.

JR



#include <dirent.h>

This is a non-standard header, hence off topic here. But see below...
struct dirent getdirlist(char *dirstart)
{
struct dirent **namelist;
int n;

n = scandir(dirstart, &namelist, 0, alphasort);
if (n < 0) perror("scandir");
return **namelist;
}

int main()

{

struct dirent *dirlist;

int c=0;
char currentdir;
currentdir='.';
dirlist = getdirlist("/root");
while(c<sizeof(dirlist))

`sizeof(dirlist)' will give you the size of the size of a pointer to a
`sturct dirent', which is *not* what you want.
{

printf("%s\n",dirlist[c]->d_name);
c=c+1;

}
}
I would advise you to post to but only
*after* you have spent some time trying to understand what a `struct
dirent' is and what it contains.

IOW, as of yet you don't have a clue. The good news is that getting one
should not be difficult.

HTH,
--ag
 
B

Barry Schwarz

I need some help. I am trying to return a dirent struct location so i
can access what the function found in main(). I dont understand
pointers very well and think that is were i am getting it wrong. If i
print everything from getdirlist everything is fine. I just cant pass
along the namelist scandir creates. Any help would be great.

Why don't you
JR



#include <dirent.h>

Non-standard so many of us will not know how the types and functions
declared here work together.
struct dirent getdirlist(char *dirstart)

getdirlist returns a struct.
{
struct dirent **namelist;
int n;

n = scandir(dirstart, &namelist, 0, alphasort);

Does scandir really take a struct dirent*** as the second argument?
Assuming it does, namelist, *namelist, and **namelist must be
initialized within that function.
if (n < 0) perror("scandir");
return **namelist;

This is the struct to be returned.
}

int main()

{

struct dirent *dirlist;

int c=0;
char currentdir;
currentdir='.';
dirlist = getdirlist("/root");

BZZT. Didn't your compiler give you a diagnostic here? getdirlist
returns a struct. dirlist is a pointer to struct. They are
incompatible types.
while(c<sizeof(dirlist))

dirlist is still a pointer. Its size is probably 4.
{

printf("%s\n",dirlist[c]->d_name);

How about another diagnostic here? dirlist is a pointer to struct.
dirlist[c] is a struct. The -> operator cannot have a left operand of
type struct. It must have one of type pointer to struct.
c=c+1;

}
}

After you correct the obvious syntax errors, you need to ask in a
group where you system specific functions are on topic. My system
doesn't have a /root but then it doesn't have directories either.


<<Remove the del for email>>
 
M

Mabden

JR said:
I need some help.

Type slower, and use all the letters.
I am trying to return a dirent struct location so i
can access what the function found in main(). I dont understand
pointers very well and think that is were i am getting it wrong. If i
print everything from getdirlist everything is fine. I just cant pass
along the namelist scandir creates. Any help would be great.

A lot of that is gibberish. Speak slowly, and explain the problem. Put
down the crack pipe, if necessary.
 

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,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top