从不兼容的指针类型&amp;分配取消引用指向不完整类型<dirent.h>的指针

时间:2017-05-27 17:54:33

标签: c

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

#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上的类似问题进行了比较

1 个答案:

答案 0 :(得分:2)

struct dirnet* file = NULL;

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

struct dirent* file = NULL;