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 9238969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:52:53+00:00 2026-06-18T07:52:53+00:00

DIR * d; int dsize=0; struct dirent * de; char *dir[1024]; d=opendir(.); while ((de

  • 0
 DIR * d;
 int dsize=0;
 struct dirent * de;
 char *dir[1024];
 d=opendir(".");
 while ((de = readdir(d)))
            {                                                                               
              if((de->d_type) & DT_DIR)
                {
                  dir[dsize]= de->d_name;
                  dsize++;
                }
             }

I’m trying to store the address of the file names into a array of char pointers.

A bit rusty on pointers I went back and read some pages of pointer review but I’m
not sure what I’m doing wrong.. Keeps telling me “warning: assignment makes integer from pointer without a cast”. Is my syntax just off because of the struct?

  • 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-18T07:52:54+00:00Added an answer on June 18, 2026 at 7:52 am

    You cannot store the pointers that way. They are overwritten every time, you call readdir and then you have a dangling pointer to invalid memory. If you want to store the dir entries, you must copy the whole name, not just the pointer

    char dir[1024][256];
    while (de = readdir(d)) {
        if (de->d_type & DT_DIR) {
            if (dsize < 1024) {
                strcpy(dir[dsize], de->d_name);
                dsize++;
            }
        }
    }
    

    Don’t forget the check for the dir array bounds. Otherwise you risk overwriting the stack, which might result in a crash.

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

Sidebar

Related Questions

Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
I've received some curious results while using opendir() : int dtw(char *path) { struct
int ScanDirectories(const char *dirname, struct images *imagesHeadPtr, struct filesToParse *filesHeadPtr) { // scan the
I'm running some commands with the C++ system() function: int system ( const char
This is a function that i've written: uint32_t file_list(char *path, char ***ls){ DIR *dp;
let rec move_robot (pos: int) (dir: string) (num_moves: int) : int = let new_forward_position
i am trying to parse a text file saved in doc dir below show
#include <stdio.h> #include <string.h> int main() { char tab[2]={12}; FILE *outfile; char *outname =
With opendir and readdir i do read a directories content. During that process i
I am accepting the path through command line input. When I do dir=opendir(args[1]); it

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.