dynamic array ?

Discussion in 'C Programming' started by David d'Angers, Oct 22, 2007.

  1. hi all

    i need a function that returns all the file names contained in a
    directory
    since the number of files is not known in advance
    how should i go about to use "dynamic array" to hold the file names ?

    can anyone post some code snippets please?
    thanks
     
    David d'Angers, Oct 22, 2007
    #1
    1. Advertising

  2. David d'Angers

    Mark Bluemel Guest

    David d'Angers wrote:
    > hi all
    >
    > i need a function that returns all the file names contained in a
    > directory
    > since the number of files is not known in advance
    > how should i go about to use "dynamic array" to hold the file names ?


    Look at the manual page for realloc...

    > can anyone post some code snippets please?


    Someone might, but I'm afraid it won't be me.
     
    Mark Bluemel, Oct 22, 2007
    #2
    1. Advertising

  3. thanks
    you've helped already
    i should be looking at dynamic memory allocation
     
    David d'Angers, Oct 22, 2007
    #3
  4. David d'Angers

    santosh Guest

    David d'Angers wrote:

    > hi all
    >
    > i need a function that returns all the file names contained in a
    > directory
    > since the number of files is not known in advance
    > how should i go about to use "dynamic array" to hold the file names ?
    >
    > can anyone post some code snippets please?
    > thanks


    One strategy might be to use an array of char *.

    char **files = malloc(WHATEVER_INITIAL_SIZE * sizeof *files);

    This sets up `files` to point to an array of char * of
    WHATEVER_INITIAL_SIZE elements.

    Now you can initialise each element in the array to point a block of
    char objects to hold each directory entry like this:

    files[ctr] = malloc(FILENAME_LENGTH * sizeof **files);

    If the number of directory entires is more than WHATEVER_INITIAL_SIZE,
    then you can use realloc to expand the array. Be sure to preserve your
    old value for files before calling realloc, since it'll return NULL on
    failure but will still leave the old block untouched.

    char **tmp = realloc(files, NEW_SIZE);
    if (tmp != NULL) files = tmp;
    /* proceed */
     
    santosh, Oct 22, 2007
    #4
  5. thanks goto santosh with all my heart
    i was just feeling confused about the fact that each element in the
    array is itself unknown
     
    David d'Angers, Oct 22, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter B. Steiger

    Can a static array contain a dynamic array of pointers?

    Peter B. Steiger, Apr 19, 2004, in forum: C Programming
    Replies:
    8
    Views:
    2,101
    Dave Thompson
    Apr 26, 2004
  2. Replies:
    2
    Views:
    1,565
    Jack Klein
    Jul 14, 2005
  3. Replies:
    2
    Views:
    566
    Victor Bazarov
    May 20, 2006
  4. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    496
    David RF
    Sep 3, 2009
  5. Ruby Student
    Replies:
    4
    Views:
    148
    Ruby Student
    Apr 9, 2009
Loading...

Share This Page