2

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
Community
  • 1
  • 1
jakehimton
  • 101
  • 8

2 Answers2

3

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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
3

usually header files only contain data definitions and function prototypes. Your function definition should almost certainly be in a C file.

If you look at the function readdir it returns a pointer to a struct dirent so your variable ent should be a pointer struct dirent *readdir(DIR *dirp);

struct dirent *ent;

That will fix your error invalid type argument of '->' (have struct dirent)

cleblanc
  • 3,678
  • 1
  • 13
  • 16