By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,411 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,411 IT Pros & Developers. It's quick & easy.

Return of struct

P: n/a
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;

}
}

Nov 27 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"JR" <87*****@gmail.com> wrote:
#
# 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;
#
# }
# }
#
#
#

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I ASSURE YOU WE'RE OPEN!
Nov 27 '05 #2

P: n/a
JR wrote:
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 news:comp.unix.programmer, 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

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Nov 27 '05 #3

P: n/a
On 26 Nov 2005 18:01:31 -0800, "JR" <87*****@gmail.com> wrote:

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>>
Nov 27 '05 #4

P: n/a
"JR" <87*****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...

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.

--
Mabden
Dec 5 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.