Chce pobrac nazwy plikow z katalogu (nie wiem, ile tych plikow moze byc w tym katalogu). Ponizszy program sie wywyala, a ja nie wiem, gdzie jest blad :/

#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

bool hasTxtExtension(char const *filename)
{
    size_t len = strlen(filename);
    return len > 4 && strcmp(filename + len - 4, ".txt") == 0;
}

char **f(const char *s)
{
    char **p = NULL;
    DIR *dir;
    struct dirent *ent;
    size_t i = 0;

    dir = opendir(s);

    while ((ent = readdir(dir)) != NULL) {
        if (hasTxtExtension(ent->d_name)) {
            p = (char**)realloc(p, (i + 1) * sizeof(char *));
            p[i] = (char*)malloc(strlen(ent->d_name) + 1);
            strcpy(p[i], ent->d_name);
            ++i;
        }
    }

    closedir(dir);
    return p;
}

int main()
{
	char **tab = f(".");
	int i=0;
	while(tab[i]!=NULL)
	{
		printf("%s\n", tab[i]);
		i++;
	}
	return 0;
}