Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > dynamic array ?

Reply
Thread Tools

dynamic array ?

 
 
David d'Angers
Guest
Posts: n/a
 
      10-22-2007
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

 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      10-22-2007
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.
 
Reply With Quote
 
 
 
 
David d'Angers
Guest
Posts: n/a
 
      10-22-2007
thanks
you've helped already
i should be looking at dynamic memory allocation

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      10-22-2007
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 */

 
Reply With Quote
 
David d'Angers
Guest
Posts: n/a
 
      10-22-2007
thanks goto santosh with all my heart
i was just feeling confused about the fact that each element in the
array is itself unknown

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
Array question - How dynamic is dynamic? Ruby Student Ruby 4 04-09-2009 12:59 PM
access object in array of pointers to dynamic array of class complex jccorreu@gmail.com C++ 2 05-20-2006 05:08 AM
Convert Static Array to Dynamic Array ? raxitsheth@gmail.com C Programming 2 07-14-2005 01:25 AM
Can a static array contain a dynamic array of pointers? Peter B. Steiger C Programming 8 04-26-2004 03:07 AM



Advertisments