1

I'm having trouble understanding the cause of this warning:

menu-file-select.c:41:29: warning: initialization from incompatible pointer type

The offending code is:

typedef int (*FileSelectFilter)(const char*, struct dirent*);

typedef struct {
    const char *dir; //the directory path to read
    const char *out; //where to copy the selected path
    int outLen; //length of out buffer
    FileSelectFilter *filter; //optional filter function
} FileSelectParams;

void showFileSelectMenu(FileSelectParams *params) {
    /* ... */
    FileSelectFilter filter = params->filter; // <-- warning generated here.
    if(filter && !filter(path, ent)) continue;
    /* ... */
}

int main(int argc, char **argv) {
    /* ... */
    FileSelectParams fsel = {
        .dir    = setting.lastpath,
        .out    = RomPath,
        .outLen = sizeof(RomPath) - 1,
        .filter = FileSelectFilter_Roms,
    };
    showFileSelectMenu(&fsel);
    /* ... */
}

int FileSelectFilter_Roms(const char *path, struct dirent *file) {
    /* ... */
}

As far as I can tell, FileSelectFilter_Roms matches the FileSelectFilter typedef, so I don't understand why I'm being told the type is incompatible. The program seems to work anyway, but having this warning here bothers me.

4
  • 3
    FileSelectFilter is already a pointer to a function (as per typedef). So in FileSelectParams, filter is defined as a pointer to a pointer to a function - drop the * ! Commented Sep 30, 2014 at 12:29
  • Why not make this answer. @isedev Commented Sep 30, 2014 at 12:31
  • very well, since I have some time available... Commented Sep 30, 2014 at 12:32
  • 2
    As an alternative (if you don't like typedefed pointers), you can typedef the function rather than a pointer to it: typedef int FileSelectFilter(const char *, struct dirent *); Commented Sep 30, 2014 at 12:43

1 Answer 1

4

You have defined FileSelectFilter as a pointer to a function in the typedef:

typedef int (*FileSelectFilter)(const char*, struct dirent*);

In FileSelectPararms, you define filter member as:

FileSelectFilter *filter;

This means that filter is actually a pointer to a pointer to a function. This is the reason why you are getting the error in the assignment FileSelectFilter filter = params->filter;.

The filter member should simply be defined as:

FileSelectFilter filter;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.