Need some advice on evil double pointer...

This is a discussion on Need some advice on evil double pointer... within the C Programming forums, part of the General Programming Boards category; It's been a while since I've coded C, and I'm just getting used to it again. I am having a ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    6

    Need some advice on evil double pointer...

    It's been a while since I've coded C, and I'm just getting used to it again. I am having a problem with a double pointer, as in a pointer that points to a list of other character pointers. My problem is that I don't understand why I can declare this double pointer in main() but not in a function:
    ----------------------------
    main () {
    char **dirnames;

    dirnames[0] = "/usr/local/share";
    dirnames[1] = "/usr/local/www";
    }
    -----------------------------
    This section of code works just fine. The following section of code does NOT work though:
    ------------------------------
    main () {
    test ("/usr/local/share");
    }
    int test (char *path) {
    char **dirnames;

    dirnames[0] = path;
    dirnames[1] = "/usr/local/www";
    }
    --------------------------------
    This section of code gives "Bus error" or segfaults, one of the two. I assume it is just a syntax problem, but if my environment information is needed, I can provide it.

    Thanks for any help.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    > This section of code works just fine.
    Yeah, it might compile, it might even run without falling over, but it isn't right at all.

    You want at least
    char *dirnames[2];

    Or if you really want
    char **dirnames;

    Then you're going to have to malloc some space for the pointers, like so
    dirnames = malloc( 2 * sizeof(char*) );

    Then (with either of them), you can do
    dirnames[0] = "/usr/local/share";
    dirnames[1] = "/usr/local/www";

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    6
    I should note that I'm new to C, and all I've coded before was C++, so I had no idea that you had to manually allocate memory for that variable =(

    It works great now when I use malloc. When do you have to use malloc? It it would be easier to point me to some online documentation, that would be great too =)

    Thanks again.

  4. #4
    Sayeh
    Guest
    Well, the reason you're confused is because you don't understand at all what is going on. Doesn't matter if it's C or C++.

    When you declare a variable of type:

    char **myVar;

    You are not declaring space for more than 4 bytes. This is a single pointer-- actually a handle. All the '**' does is tell the compiler that this is a pointer to another pointer.

    This is _not_ an array.

    Salem's advice shows you how to create an array of pointers.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    6
    Wow, I went from forgetting to allocate memory for a variable to "you don't understand at all what is going on." Thanks for the 'constructive' reply...

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    Jhopper - read this (several times)
    http://pw1.netcom.com/~tjensen/ptr/pointers.htm

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    6
    Ahhh, that was a good refresher course. I don't think I ever quite grasped the concept of double pointers, and that tutorial was excellent.

    One last question, on a problem that I can't quite figure out (this goes out to anyone who has used fts):

    Is there some call to malloc that must be made to retrieve an fts structure when it is used deep inside a program? The following code works fine when used in main, but not when it is called by a function that is called by a function, etc...
    ------------------------------
    long get_du(char *path) {
    FTS *fileheir;
    FTSENT *fsentry;
    char **dirnames;
    unsigned long size = 0;

    dirnames = malloc(sizeof(char*));
    dirnames[0] = path;

    fileheir = fts_open(dirnames, FTS_PHYSICAL, NULL);
    while ((fsentry = fts_read(fileheir)) != NULL) {
    if (fsentry->fts_number == 0) {
    size = size + fsentry->fts_statp->st_size;
    fsentry->fts_number = 1;
    }
    }
    fts_close(fileheir);
    return size;
    }
    ----------------------------------
    The call to fts_open takes a double pointer as an argument, hence why I asked about it. I have a feeling there might be an easier way to do it though...

    (prepares self for ignorance flame....)

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    > dirnames = malloc(sizeof(char*));
    > dirnames[0] = path;
    This isn't what the spec says
    It says that there must be a NULL pointer

    dirnames = malloc ( sizeof(char*) * 2 );
    dirnames[0] = path;
    dirnames[1] = NULL;

    > The following code works fine when used in main
    Mmm, since this function uses malloc, any mis-use of malloc on your part earlier on will compromise this function.

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    6
    I had the second element of dirnames pointing to NULL at first, so I'll try that again.

    I'm modifying someone else's program, so I can only hope that they used malloc correctly. Well, I guess I could go through the rest of the code and try to make sure that they did.

    Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. pointer with double asterisk
    By skringla in forum C Programming
    Replies: 10
    Last Post: 11-27-2008, 06:33 AM
  3. expected primary expression
    By mju4t in forum C Programming
    Replies: 2
    Last Post: 03-27-2007, 06:59 PM
  4. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 08:13 AM
  5. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21