Return of struct

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JR

    #1

    Return of struct


    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(dirstar t, &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",d irlist[c]->d_name);
    c=c+1;

    }
    }

  • SM Ryan

    #2
    Re: Return of struct

    "JR" <87325is@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(dirstar t, &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",d irlist[c]->d_name);
    # c=c+1;
    #
    # }
    # }
    #
    #
    #

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

    Comment

    • Artie Gold

      #3
      Re: Return of struct

      JR wrote:[color=blue]
      > 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>[/color]

      This is a non-standard header, hence off topic here. But see below...[color=blue]
      >
      >
      > struct dirent getdirlist(char *dirstart)
      > {
      > struct dirent **namelist;
      > int n;
      >
      > n = scandir(dirstar t, &namelist, 0, alphasort);
      > if (n < 0) perror("scandir ");
      > return **namelist;[/color]
      [color=blue]
      > }
      >
      > int main()
      >
      > {
      >
      > struct dirent *dirlist;
      >
      > int c=0;
      > char currentdir;
      > currentdir='.';
      > dirlist = getdirlist("/root");
      > while(c<sizeof( dirlist))[/color]

      `sizeof(dirlist )' will give you the size of the size of a pointer to a
      `sturct dirent', which is *not* what you want.[color=blue]
      >
      > {
      >
      > printf("%s\n",d irlist[c]->d_name);
      > c=c+1;
      >
      > }
      > }
      >[/color]
      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
      I will (ir)regularly write about things that are important to me -- that I hope interest you. Of course, since I see most things as being political, that will be most of it.

      http://www.cafepress.com/goldsays
      "If you have nothing to hide, you're not trying!"

      Comment

      • Barry Schwarz

        #4
        Re: Return of struct

        On 26 Nov 2005 18:01:31 -0800, "JR" <87325is@gmail. com> wrote:
        [color=blue]
        >
        >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.[/color]

        Why don't you[color=blue]
        >
        >JR
        >
        >
        >
        >#include <dirent.h>[/color]

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

        getdirlist returns a struct.
        [color=blue]
        >{
        > struct dirent **namelist;
        > int n;
        >
        > n = scandir(dirstar t, &namelist, 0, alphasort);[/color]

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

        This is the struct to be returned.
        [color=blue]
        >}
        >
        >int main()
        >
        >{
        >
        > struct dirent *dirlist;
        >
        > int c=0;
        > char currentdir;
        > currentdir='.';
        > dirlist = getdirlist("/root");[/color]

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

        dirlist is still a pointer. Its size is probably 4.
        [color=blue]
        >
        > {
        >
        > printf("%s\n",d irlist[c]->d_name);[/color]

        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.
        [color=blue]
        > c=c+1;
        >
        > }
        >}[/color]

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

        Comment

        • Mabden

          #5
          Re: Return of struct

          "JR" <87325is@gmail. com> wrote in message
          news:1133056891 .534216.39440@f 14g2000cwb.goog legroups.com...[color=blue]
          >
          > I need some help.[/color]

          Type slower, and use all the letters.
          [color=blue]
          > 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.
          >[/color]

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

          --
          Mabden


          Comment

          Working...