gpt4 book ai didi

c - 从不兼容的指针类型赋值并取消引用指针到不完整类型

转载 作者:行者123 更新时间:2023-11-30 18:48:54 24 4
gpt4 key购买 nike

我正在尝试编写一个程序来读取有关程序所在文件夹的信息。我已经仔细检查了其他在线资源,看起来我正在使用与其他正确解决方案相同的模式(见下文)。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>

void append(char* path, char* cur)
{
strcat("/",cur);
strcat(cur,path);
}

void addDot(char* dot)
{
int len = strlen(dot);
dot[len] = '.';
}

int main()
{
struct stat st;
int stDevCur = st.st_ino;
int stDevNew = 0;
int inodeCur = st.st_dev;
int inodeNew = 0;
char* path = "";
char* new = "";
char* cur = "_";
char* dot = ".";
DIR* dir;
struct dirnet* file = NULL;

if((dir = opendir(dot)) == NULL)
{
perror(NULL);
exit(EXIT_FAILURE);
}
while(strcmp(cur,new) != 0)
{
if((file = readdir(dir)) == NULL)
{
perror(NULL);
exit(EXIT_FAILURE);
}
while(file != NULL)
{
inodeNew = st.st_ino;
stDevNew = st.st_dev;
if(inodeNew == inodeCur && stDevNew == stDevCur)
{
cur = file->d_name;
append(path, cur);
break;
}
}
fprintf(stderr,"%s\n",path);
}
return 0;
}

报错如下

main.c: In function ‘main’:
main.c:42:16: error: assignment from incompatible pointer type [-Werror]
if((file = readdir(dir)) == NULL)
^
main.c:53:23: error: dereferencing pointer to incomplete type
cur = file->d_name;
^
cc1: all warnings being treated as errors
make: *** [main.o] Error 1

我从昨晚开始就一直在研究这个问题,并将它们与 stackoverflow 上的类似问题进行比较

最佳答案

struct dirnet* file = NULL;

问题是拼写错误。应该是:

struct dirent* file = NULL;

关于c - 从不兼容的指针类型赋值并取消引用指针到不完整类型 <dirent.h>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44220013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com