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

  • Home
  • SEARCH
  • 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 6562781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:45:01+00:00 2026-05-25T13:45:01+00:00

I want a function that returns the content of a given directory. For this,

  • 0

I want a function that returns the content of a given directory. For this, I am using scandir from dirent.h. The code below compiles successfully (gcc -Wall test.c), but the last printf leads to a segmentation fault. It means that the “eps” structure (a pointer to an array of pointers to dirent structures) is still empty after the function: how can I fix this?

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

static int myselector(const struct dirent * dir_entry)
{
  char * pch = strstr(dir_entry->d_name, ".");
  return pch == NULL ? 1 : 0;
}

int list_dir(char * dirname, struct dirent ** eps)
{
  int nbfiles = scandir(dirname, &eps, myselector, alphasort);
  if(nbfiles > 0)
  {
    printf("inside function: %s\n", eps[0]->d_name);
    return 1;
  }
  else
    return 0;
}

int main(int argc, char *argv[])
{
  int status = 0;
  struct dirent ** eps = NULL;
  status = list_dir("/home", eps);
  if (status)
  {
    puts("ok");
    printf("outside function: %s\n", eps[0]->d_name);
  }
  return EXIT_SUCCESS;
}
  • 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-05-25T13:45:02+00:00Added an answer on May 25, 2026 at 1:45 pm

    Because your pointer has changed, and you’re looking at the wrong thing in main() 🙂

    You’re passing a pointer to a pointer to a pointer to scandir(). it’s changing what the pointer to a pointer is pointing at (I know, that hurts to read … ).

    Because you’re calling scandir() with &eps in your function, you lose that change outside of the function. The value of eps has changed inside your function.

    To better understand this, in your current function wrap the scandir() call with printf() statements showing you what the value contained in eps is:

    ...
    printf("%p\n", eps);
    int nbfiles = scandir(dirname, &eps, myselector, alphasort);
    printf("%p\n", eps);
    ...
    

    To fix this change your function to:

    int list_dir(char * dirname, struct dirent *** eps)
    {
      int nbfiles = scandir(dirname, eps, myselector, alphasort);
      if(nbfiles != -1)
      {
        printf("inside function: %s\n", (*eps)[0]->d_name);
        return 1;
      }
      else
        return 0;
    }
    

    And call it as …

    status = list_dir("/home", &eps);
    

    in main(). It will then work perfectly:

    broach@roach-VirtualBox:~$ ./test
    inside function: broach
    ok
    outside function: broach

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

Sidebar

Related Questions

I'm using R5RS Scheme and I just want to implement a function that returns
I want to write a function that returns the nearest next power of 2
Let's imagine I want to make a templated function that returns the first element
I want a simple function that receives a string and returns an array of
I want a python function that takes a pdf and returns a list of
I want to write a function in Python that returns different fixed values based
I want a function/code which will return the value that the user submitted for
I want to have a function that will return the reverse of a list
I have 1 function that I want to return the address of an assigned
I just want a function that can take 2 parameters: the URL to POST

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.