如何在声明 const 字符并将其作为参数传递给函数后将指针值分配给 const char**



这是我必须使用readdir()从目录中获取文件的程序,之后我需要通过const char**返回read_files()函数中的文件名,稍后我需要使用该文件名使用 getfile() 读取它 - 在这里我需要将其作为参数中的const char*传递, 不知道该怎么做?

我收到此错误warning: assignment from incompatible pointer type [enabled by default]

typedef struct{
  DIR* path;
  struct dirent* readf;
} readfiles;
int read_files(readfiles *r, const char** file){
   if(readfiles->readf = readdir(readfiles->path)) != NULL){
     file = &(readfiles->readf->d_name);
   }
 return 0;
}
int getfile(readfiles *r, const char* filename){
  int fd = open(filename, O_RDONLY);
  close(fd);
  return 0;
}
int main(){
 const char** filename;
 readfiles r;
 read_files(&r, filename);
 getfile(&r, *filename);
 return 0;
}

假设其余函数是正确的并且readfiles->readf->d_name指向 C 字符串

int read_files(readfiles *r, char** file){
   if(readfiles->readf = readdir(readfiles->path)) != NULL){
     strcpy(*file, readfiles->readf->d_name);  // <-- copy 
   }
 return 0;
}
}
int main(){
 char fbuff[MAXFILENAME];
 char *filename = fbuff;
 readfiles r;
 read_files(&r, &filename);
 getfile(&r, *filename);
 return 0;
}

最新更新