Search code examples
cpointersstructurereaddir

What is wrong with this header file?


I'm fairly new to C and am starting to learn header files. Whilst using my header I'm getting an error saying invalid type argument of '->' (have struct dirent). I don't understand what this means, I read here that the second argument to -> must be a pointer, so I tried to add a * to it (ent->*d_name) however then I get the error unexpected token *, how can I fix this?

#ifndef UTILIS_H_INCLUDED
#define UTILIS_H_INCLUDED "utilis.h"
#include <stdio.h>
#include <dirent.h>

char *connect(const char *pattern)
{
    struct dirent ent;
    char *d_name;

    DIR *mgt = opendir("\\\\example\\windows7apps");

    while ((ent = readdir(mgt)) != pattern)
    {
        puts(ent->d_name);
    }
}

#endif

Solution

  • I read here that the second argument to -> must be a pointer,

    That's wrong, the "first" argument, or, actually, the operand of the -> operator should be of pointer type.

    In your case, ent is not a pointer type, so you cannot use the pointer member dereference operator ->. (you could have used the member dereference operator . instead).

    Actually, in your code, ent should be a pointer, as per the return type of readdir(). So you better correct the type of ent to be of struct dirent *, then you can make use of -> on ent.