Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9182049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:28:05+00:00 2026-06-17T18:28:05+00:00

I’m trying to list all files and folders in a given directory in C,

  • 0

I’m trying to list all files and folders in a given directory in C, the following code errors out and i cant figure out whats wrong

#include <sys/types.h>
#include <dirent.h>
#include <regex.h>
#include <stdio.h>

#include <unistd.h>
#include <pwd.h>

enum {
    WALK_OK = 0,
    WALK_BADPATTERN,
    WALK_BADOPEN,
};

int walk_directories(const char *dir, const char *pattern, char* strings[])
{
    struct dirent *entry;
    regex_t reg;
    DIR *d; 
    int i = 0;
    //char array[256][256];

    if (regcomp(&reg, pattern, REG_EXTENDED | REG_NOSUB))
    return WALK_BADPATTERN;
    if (!(d = opendir(dir)))
    return WALK_BADOPEN;
    while (entry = readdir(d))
    if (!regexec(&reg, entry->d_name, 0, NULL, 0) )
            //puts(entry->d_name);
        strings[i] = (entry->d_name);
        i++;
    closedir(d);
    regfree(&reg);

    return WALK_OK;
}

void main()
{
    struct passwd *pw = getpwuid(getuid());
    char *homedir = pw->pw_dir;
    strcat(homedir, "/.themes");

    int n = 0;
    char *array[256][100];
    char *array2[256][100];

    walk_directories(homedir, "", array);
        for (n = 0; n < 256; n++)
        {
            //do stuff here later, but just print it for now
            printf ("%s\n", array[n]);
        }

    walk_directories("/usr/share/themes", "", array2);
        for (n = 0; n < 256; n++)
        {
            //do stuff here later, but just print it for now
            printf ("%s\n", array2[n]);
        }
}

The error at compile time is

test2.c: In function ‘main’:
test2.c:42:2: warning: incompatible implicit declaration of built-in function ‘strcat’ [enabled by default]
test2.c:48:2: warning: passing argument 3 of ‘walk_directories’ from incompatible pointer type [enabled by default]
test2.c:15:5: note: expected ‘char **’ but argument is of type ‘char * (*)[100]’
test2.c:52:6: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat]
test2.c:55:2: warning: passing argument 3 of ‘walk_directories’ from incompatible pointer type [enabled by default]
test2.c:15:5: note: expected ‘char **’ but argument is of type ‘char * (*)[100]’
test2.c:59:6: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat]

If it helps, I’ve implemented what I want already in python, this is the desired result for C

import os
DATA_DIR = "/usr/share"

def walk_directories(dirs, filter_func):
    valid = []
    try:
    for thdir in dirs:
        if os.path.isdir(thdir):
            for t in os.listdir(thdir):
                if filter_func(os.path.join(thdir, t)):
                     valid.append(t)
    except:
    logging.critical("Error parsing directories", exc_info=True)
    return valid

def _get_valid_themes():
    """ Only shows themes that have variations for gtk+-3 and gtk+-2 """
    dirs = ( os.path.join(DATA_DIR, "themes"),
         os.path.join(os.path.expanduser("~"), ".themes"))
    valid = walk_directories(dirs, lambda d:
            os.path.exists(os.path.join(d, "gtk-2.0")) and \
            os.path.exists(os.path.join(d, "gtk-3.0")))
    return valid

print(_get_valid_themes())

thank you

[EDIT]
thanks for the help, only problem im having now is the printf’s all spit out rubbish instead of what i expected, ive tried a few things and the while loop looks like this now

    while (entry = readdir(d))
    if (!regexec(&reg, entry->d_name, 0, NULL, 0) )
            //printf("%s\n",entry->d_name);
        strcpy(strings[i], (entry->d_name));
        //strings[i] = (entry->d_name);
        printf("%i\n",i);
        i++;
    closedir(d);

the i doesnt get printed properly either, this is all i get from the 3 printf statements

0
Adwaita2





















\@




0
Radiance

��





\@



�K��
� `���
����
�


��
�
.N=

�O��
�

�

should mention that if i enable

       printf("%s\n",entry->d_name);

then it prints the expected output though

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:28:06+00:00Added an answer on June 17, 2026 at 6:28 pm
    1. You should include string.h to get the declaration of strcat(3).

    2. In your declaration:

      int walk_directories(const char *dir, const char *pattern, char* strings[])
      

      The char *strings[] is just syntactic sugar meaning char **strings. Since you’re passing a 2D array, that won’t work. It looks to me like you’re intending to make two arrays of strings, but that’s not what these declarations do:

      char *array[256][100];
      char *array2[256][100];
      

      You probably don’t want the *s there. If you take them off, you can change the signature of walk_directories to this:

      int walk_directories(const char *dir, const char *pattern, char strings[][100])
      

      And it should work, with the necessary changes inside your function to match. As a bonus, this change will make your printf calls start working, too.

    3. It looks like you’re missing some braces around your while loop body.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.