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

1. ## 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) -.

2. 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:

Code:
```
static char* str[1];

...

char **get_imagenames(const char *path, char* mask) {

}

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);
exit(EXIT_SUCCESS);
} else {
while((foo = readdir(dir_ptr)) != NULL) {
extension = (char *) &foo->d_name[find_chr_lastpos(
foo->d_name,'.')];

/* 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:
str[m] = (char *) xmalloc(foo->d_namlen + 1);
_snprintf(str[m],foo->d_namlen+1,"%s",foo->d_name);
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);
_snprintf(str[m],foo->d_namlen+1,"%s",foo->d_name);
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. */

Code:
``` char *bstr[m];
if(str[0] != NULL) {
memmove(bstr,str,sizeof(str));
size_t cc = sizeof(bstr) / sizeof(bstr[0]);
qsort(str,cc,sizeof(str[0]),alpha_compare);
}

(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);
exit(EXIT_SUCCESS);
}
}```
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.