I am struggling with a segmentation fault that occurs when the dns variable is changed from size 10 to size 32. I believe the declaration of the dns ariable in main is creating an array of 10 strings, and then when increased to 32, an array of 32 strings.
So when executed with dns variable of size 10, I get output of 4 directory names starting with the letter D. This is the expected output.
If I increase dns variable to size 32, I still get the 4 directory names and then immediately afterwards, a Segmentation fault.
It has been a very long time since I have coded and I am sure I am violating memory somewhere.
Any help is greatly appreciated.
Thanks,
Dan.
void ld_dirs(char *strings[])
{
int count;
DIR *dp;
struct dirent *ep;
dp = opendir ("/tmp/sysinfo/inputs");
if (dp != NULL)
{
count = 0;
while (ep = readdir (dp))
{
if (ep->d_name[0] == 'D')
{
strings[count] = malloc (80 * sizeof(char));
strings[count] = ep->d_name;
count ++;
}
}
closedir (dp);
}
else
perror ("Could not open the directory");
}
int main ()
{
char cwd[120];
int count, count2;
char *dns[10];
char *path;
if (getcwd(cwd, sizeof(cwd)) != NULL)
printf ("\nCurrent working directory : %s\n\n\n", cwd);
else
perror ("getcwd() error");
ld_dirs (dns);
count = 0;
puts ("List of valid inputs :\n");
while (dns[count] != NULL)
{
puts (dns[count]);
count ++;
}
printf ("There are %d valid inputs\n", count);
return 0;
}