Thread: Variable-sized Arrays. Help! -:)

  1. #1
    Registered User
    Join Date
    Aug 2002

    Question Variable-sized Arrays. Help! -:)

    Hi Everybody!

    I've got an array which was built by a function called getdir().
    This reads in the content of a directory (one filename at a time),
    puts each element into the array, then it sorts the array
    alphabetically via qsort().

    Does anybody know how to get the real size of this array if
    it was declared as:

    char **arr; (this is a global var).

    I tried sizeof(arr) but it says the size is 4, which is
    true only if the array has 1 element. Therefore I can't
    get the number of elements with "sizeof(arr) / sizeof(arr[x])",
    as this will also be 1 in every case.

    I know that the array could be declared as

    char *arr[a bigger number];

    However, this is not good either, because this method
    will allocate space for a much bigger array than what I need.

    My teacher told me that this is a limitation of C, and it
    could be solved only by implementing a "special programming
    technique". Unfortunately he wasn't very specific about
    the details..

    So, what do you think, is this true? If yes, could anyone tell
    how to solve this?

    Any suggestions would appreciated. I'm completely
    confused) -.

    Please help me out guys!

    Thanks in Advance.
    Best Regards,


  2. #2
    Registered User
    Join Date
    Aug 2002
    Hi Salem,

    First of all, sorry for the late response.

    Of course I'm using malloc (or xmalloc), but I have used it to allocate mem for the elements of the array, since I don't know how much memory is required for the array itself. This is because my function reads in names of image files from a dir, however the total number of these is unknown, so the final size of the
    array cannot be calculated (or at least, I don't know how to do it...-.

    Here is the code:

       static char* str[1];
       char **get_imagenames(const char *path, char* mask) {                           
       bool masked = mask == NULL; 
       if(masked != 1) { 
       mask = replace_chr(mask,'*',NULL,CASE);
       char* extension = (char *) path;                  
       DIR *dir_ptr; int n = 0, m = 0, b = 0;
       struct dirent *foo; str[m+1];
       dir_ptr = opendir(path);
       char* foo_ptr;
       if(dir_ptr == NULL) {
       printf("\nError: could not open dir '%s'!\n",path);
       } else {
       while((foo = readdir(dir_ptr)) != NULL) { 
       extension = (char *) &foo->d_name[find_chr_lastpos(
       switch(masked) {
      /* There are two possibilites.: If the file mask is defined   (case1),   then the func. will read in only those filenames whose extension is '*.mask'. Otherwise it makes an attempt to look up the extension in the array 'supported', in case of success the filename will be fetched, } else { -:) it'll be ignored. */
       case 0:
       if(strcasecmp(extension,mask) == 0) {
       str[m] = (char *) xmalloc(foo->d_namlen + 1);
       b = 1; m = m++; } break; 
       case 1:
       if(opendir(foo->d_name) == NULL && array_search(extension,
       1,0,0,supported,NO_CASE) > 0) {
       str[m] = (char *) xmalloc(foo->d_namlen + 1);
       b = 1; m = m++; } break; }
       n = n++;
    /* str is the array. Theoretically, now it contains 0 or more
    elements, but if I try to count them with sizeof(str) / sizeof(str[0]), it says there is only 1 element and the size of str is 4. */

    /* This part here is has no point at all. Qsort() needs the array size, but this will be always m, so size_t cc and char *bstr[m] are not required. I've declared them just to make some expirements with qsort. */

     char *bstr[m];
     if(str[0] != NULL) { 
     size_t cc = sizeof(bstr) / sizeof(bstr[0]);
      (void) closedir(dir_ptr);   
      if(b != 0) {
      return str;
      } else {
      printf("\nError: '%s' dir doesn't conatin the required " 
      "type of img files or there are no image files at all.\n\n,path);
    I can't figure out why the size of str[] is 4. Because I've declared as ... str[1]? But how do I know what size to specify here? Please advise.

    Thanks in advance!

    [code][/code]tagged by Salem
    Best Regards,


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global and static variable in a class delivered in a DLL
    By George2 in forum C++ Programming
    Replies: 16
    Last Post: 04-13-2008, 08:19 AM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  3. Array sizes and arbitrary sized arrays
    By bennyandthejets in forum C++ Programming
    Replies: 3
    Last Post: 07-03-2003, 11:24 PM
  4. Beginner question
    By Tride in forum C Programming
    Replies: 30
    Last Post: 05-24-2003, 08:36 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM