while( (entry = readdir(dir_pointer)) != NULL)
char *fullpath = malloc(dir_len + strlen(entry->d_name) + 2);
printf("\nfullpath: %s\n", fullpath);
strcat(fullpath, dir); // concatenate file directory;
strcat(fullpath, "/"); // concatenate "/";
strcat(fullpath, entry->d_name); // concatenate filename;
printf("\nfullpath: %s\n", fullpath); // print to check;
// close the file;
fullpath is not initialized. You should not use and you don't need to use
strcat() in this situation, but if you want it to work simply make the first parameter of
strcat() a valid string, it can be an empty string.
if (fullpath == NULL) exit(-1); fullpath = '\0';
after that, you can use
fullpath as the first parameter to
strcat() is bad to use because it scans the first paramter checking for the
null terminator but, in your case the contents of
fullpath are undeterminate and it might happen that the first character is a
'\0' but there is no guarantee.
You can see how, searching for the
'\0' terminator every time you pass the string to
strcat() which is presumably growing will be inefficient.
Also, since you are going to
fullpath inside the loop, don't use
malloc() at all. You can use a VLA (Variable length array) or a fixed length one too. Calling
free() is not cheap and you will already need
strcpy() or better
snprintf() in this case is more suitable.