Thread: Structs question

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. strtok is destructive, so if you plan to do this you'll need to copy command somewhere.
    2. You should not depend on the fact that int and char * are the same size in your compiler; if you mean sizeof(char*), then say sizeof(char*).
    3. strtok returns pointers to current memory, so you don't need to malloc args[i].

  2. #17
    Registered User
    Join Date
    Nov 2007
    Posts
    39
    Ok got it.

    Now with a question for actually creating directories with function mkdir():
    When the user enters a mkdir command, I call the mkdir() function:
    Code:
    void mkdir(Funix *funix, int argCount, char *args[]);
    funix is a pointer to the Funix struct, argCount is the number of arguments (including command name itself), and args is an array of strings containing each argument entered. Since I'm on the 2nd page now I'll type again what my Funix and Directory structs looked like:
    Funix struct:
    Code:
    typedef struct
    {
      Directory *currentDirectory;
      int umask;
      int time;
    } Funix;
    Directory struct:
    Code:
    typedef struct Dir
    {
      char *name;
      int mtime;
      struct Dir **subDir;
      int numSubDir;
      struct Dir *parent;
      Permissions *perm;
    } Directory;
    The program is supposed to create the subdirectory specified in the command by the user UNLESS there are already 3 subdirectories in the current directory (so maximum number of subdirs in each directory is 3). subDirs is supposed to be a "pointer to a dynamically allocated array of pointers to its immediate subdirectories." Right now I haven't done anything with subDirs in my code, but I read the specification again and does that mean subDirs should be a triple pointer (pointer to a dynamically allocated array of pointers) instead of what I have now? If so, how would I create subdirs? I was thinking something like this:
    Code:
    (*(*funix).currentDirectory).subDirs = (Directory**)malloc(sizeof(Directory));
    If so, how would I access each subDir? Just by doing (*(*funix).currentDirectory).subDirs[1], and so on? And I would also be able to acces funix->currentDirectory->subDirs->name and the other members of the Directory struct? Sorry for the long post, there just seems to be so many pointers I'm a little confused just reading the specification. Thanks!

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have subDir correct. You would create it by saying something like
    Code:
    subDir = malloc(numSubDir * sizeof(struct Dir *);
    This way you would have room for numSubDir directory-pointers in the subDir name. So if numSubDir was three, say, you would have subDir[0], subDir[1], and subDir[2]; each would be a struct Dir *, but when you make a directory you would create the struct then and then let the parent's subDir point to it.

    And yes, you would use -> to get elements of the struct Dir pointed to by subdir[1], a la
    funix->currentDirectory->subDir[1]->name.

  4. #19
    Registered User
    Join Date
    Nov 2007
    Posts
    39
    Ahh ok, so I managed to dynamically allocate subDirs, but I'm getting an error when trying allocate subDir.name:
    Code:
    (*(*funix).currentDirectory).subDir = (Directory**)malloc(3*sizeof(struct Dir *));
      (*(*(*funix).currentDirectory).subDir[0]).name = (char*)malloc(80*sizeof(char));
    The 2nd line is causing the program to segfault when it reaches it. I also tried this:
    Code:
    funix->currentDirectory.subDir[0]->name = (char*)malloc(80*sizeof(char));
    Those two are supposed to be equivalent? What am I doing wrong here?

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Remember, subDir is an array of pointers. Your malloc has given you the space to hold those pointers -- but you don't have a struct Dir yet! You have a pointer that you can set to one, once you get one.

  6. #21
    Registered User
    Join Date
    Nov 2007
    Posts
    39
    Ok I got this to work!:
    Code:
    (*(*funix).currentDirectory).subDir = (Directory**)malloc(3*sizeof(struct Dir *));
      funix->currentDirectory->subDir[0] = (Directory*)malloc(sizeof(Directory));
      funix->currentDirectory->subDir[0]->name = (char*)malloc(80*sizeof(char));
    I tried using strcpy() to store a test string in subdir.name and it worked. One question I have though is in the second line, i called malloc() with sizeof(Directory). Although this seemed to work I'm not sure if that's the right size I should be using. If it is can you explain why? I'm having trouble wrapping my head around what needs to be the correct size to allocate here.

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should always always always always malloc the size of the thing pointed TO, which is either
    Code:
    foo = malloc(sizeof *foo);
    for a single thing or
    Code:
    foo = malloc(num_in_array * sizeof *foo);
    for a dynamic array of things (see subDir above for example, 3 in the array, and the thing pointed to was a struct Dir *, so...). In this case, the thing pointed to was a Directory, so there you go.

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I forget, but wouldn't it be easier with a single array? A pointer to the array instead of a pointer to the pointer of the array.
    And remember: -> is the same as "(*name)." So, in other words, if you want to dereference a pointer and access its members or just access a pointers members, use ->.
    mystruct* p;
    p->mymember
    instead of
    (*p).mymember

    It gets a lot better the more pointers you have.
    Instead of
    mystruct* p;
    *(*(*(*p).mymember1).mymember2).mymeber3 = something;
    Do
    p->mymember1->mymember2->mymember3 = something;

    It sure looks better to me.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    Nov 2007
    Posts
    39
    Ok, I've encountered another problem in this program. So far I've implemented mkdir() and ls() that both work, however I've been trying for the past day to be able to do this: When the program first starts, the user is given this prompt:
    Code:
     / #
    They type a command, lets say "mkdir mydirectory". Then I type cd mydirectory at the prompt, at which point the prompt should update to this:
    Code:
    /mydirectory/ #
    Then if I call "mkdir test" and then "cd test" the prompt should update to this:
    Code:
    /mydirectory/test/ #
    However, I've only been able to get the prompt to display "test #" and not the full path name ("/mydirectory/test"). I think this is a problem with how I've implemented mkdir() and cd(). Here's what I did: When the user types in "mkdir directory_name" I create the appropriate funix->currentDirectory->subDir structs. When the user types cd, my cd() function is called and I basically make a copy of the Funix struct and then change funix->currentDirectory->name to reflect the name of the directory I cd into. This of course does not allow me to show the full path name of a directory at the prompt. I've noticed that in my Directory struct there is a "struct Dir *parent" member which is supposed to be a pointer to the parent directory. I haven't found how to implement this into my program yet. Does anyone have any ideas and can give me any hints on how I can use this to print the full path of a directory (even when its nested inside many other directories)? thanks again

  10. #25
    Registered User
    Join Date
    Apr 2008
    Posts
    1
    oook...u must be in Sean's class! ok i gotta question for u i'm totally stuck something stupid, but how to you write the function ls??

  11. #26
    Registered User
    Join Date
    Apr 2008
    Posts
    1
    To list the path, you want to recursively list the parent directory for each directory, starting at the current directory.

    - foosh

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about array of structs...
    By Huskar in forum C Programming
    Replies: 2
    Last Post: 03-30-2009, 06:36 AM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Replies: 5
    Last Post: 02-20-2004, 09:36 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM