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?
You cannot store the pointers that way. They are overwritten every time, you call
readdirand 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 pointerDon’t forget the check for the
dirarray bounds. Otherwise you risk overwriting the stack, which might result in a crash.