The code of the faulty function is:
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#define _ISDIR 1729
#define _ISFILE 431
#define _EXIST_ERR 611
#define _BUF_LEN 512
unsigned int fod(char*);
unsigned int fod(char *name){
DIR check_dir;
check_dir=opendir(name);
int openf=open(name,O_RDONLY);
if(check_dir!=NULL){
return _ISDIR;
}
if(openf!=-1){
return _ISFILE;
}
return _EXIST_ERR;
}
When I compile it with gcc 12.1.1, I get the following error:
copy.c:14:9: error: storage size of ‘check_dir’ isn’t known
14 | DIR check_dir;
| ^~~~~~~~~
How do I fix it? the function parameters are right, everyone says that this is how a directory should be opened. How do I fix it?
CodePudding user response:
It doesn't compile because DIR
struct is not fixed and you need to use a pointer to DIR
instead. Also, opendir
returns *DIR
too. So replace this:
DIR check_dir;
With this:
DIR *check_dir;
You can check the return type by reading opendir
manual page. The function is declared like this:
DIR *opendir(const char *name);
(Notice the return type)
CodePudding user response:
You can't declare an object (variable) of type DIR
- like the FILE
type, the definition of DIR
is hidden. You can only declare a pointer to DIR
:
DIR *check_dir = opendir(name);