Thread: Just a questions regarding structures

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    69

    Just a questions regarding structures

    I just have a question regarding structures. I have commented the source accordingly. Cheers.

    Code:
    #include<dirent.h>
    #include<stdio.h>
     
    int main(void)
     {
        DIR            *d;
        struct dirent  *dir; //How are these structures defined when there isn't a stucture set?
        d = opendir("."); // and why is dir a pointer?
     
     
        if (d)
        {
              while ((dir = readdir(d)) != NULL)
              {
                 printf("%s\n", dir->d_name);
              }
          }
          
    closedir(d);
    getchar();
    
    
    return 0;
    }
    Last edited by inu11byte; 02-17-2012 at 04:48 PM.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Wat? Copied from here? Unix shell scripts (C)

    > How are these structures defined when there isn't a stucture set?
    What's a structure set?

    >
    and why is dir a pointer?
    Because opendir() returns a pointer.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    They're defined in dirent.h, which is a header file defined on *nix systems. Read the man pages for opendir, readdir and closedir for more info. It's a pointer because that's how they designed it, probably​ so they could allocate memory and return that. You would have to ask whoever wrote it for more info.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Quote Originally Posted by memcpy View Post
    Wat? Copied from here? Unix shell scripts (C)

    > How are these structures defined when there isn't a stucture set?
    What's a structure set?

    >
    and why is dir a pointer?
    Because opendir() returns a pointer.
    What do you mean it returns a pointer? Could you give a brief explanation?

    And the code has
    Code:
     structdirent  *dir;
    but there is no structure created. E.g. I expected to see
    Code:
    struct dirent{
        char name[20];
        int age;
        float salary;
    }
    Something along those lines...

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Maybe you'd see it if you looked (well enough into) dirent.h, but you aren't supposed to care what that contains. It's a library for working with directories, and much of the programming interface (the part that you interact with) merely expects you to provide a pointer to an initialized dirent structure. The opendir function initializes said structure and returns a pointer to it. The closedir function frees the pointer.

    Also I'm really amused that you think a dirent structure is like a salaryman.
    Last edited by whiteflags; 02-17-2012 at 05:13 PM.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    So why could a structure be used when a structure wasn't defined with elements?

    EDIT: Just looked in the dirent.h file and saw that it has the structure defined in there. Thanks for all the help. *facepalm*
    Last edited by inu11byte; 02-17-2012 at 06:27 PM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by inu11byte View Post
    So why could a structure be used when a structure wasn't defined with elements?
    It was defined with elements, in dirent.h. When you #include <dirent.h>, the compiler pastes the contents of dirent.h directly into your C file at that point, so it knows about the elements of the struct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Solution Needed] Structures questions
    By yikchong in forum C Programming
    Replies: 3
    Last Post: 10-31-2009, 11:37 AM
  2. Data structures basic questions
    By WarDoGG in forum C Programming
    Replies: 7
    Last Post: 07-11-2008, 09:42 AM
  3. A questions about structures
    By HAssan in forum C Programming
    Replies: 1
    Last Post: 04-28-2007, 07:41 PM
  4. COntrol structures and functions questions
    By angelicscars in forum C Programming
    Replies: 1
    Last Post: 11-21-2005, 11:50 AM
  5. I have some questions about data structures and algorithms
    By Tonyukuk in forum C++ Programming
    Replies: 4
    Last Post: 03-27-2003, 01:31 PM