-2

I have made inotifyFunc to monitor two paths. Nothing is giving me an error except the last line [the code that I have taken from the main function] that is monitor.wd[0]. The error is

warning: passing argument 3 of ‘inotifyFunc’ makes pointer from integer without a cast

I don't know what is the problem? Although the above line monitor.wd[0] = *pathname1; didn't give me an error.

void inotifyFunc(char *path, uint32_t *maskPtr, int *wd[2]){
    monitor.fd = inotify_init();
    if(fcntl(monitor.fd, F_SETFL, O_NONBLOCK)){
       perror("inotify not initialized: ");
       exit(0);
    }

    *wd[0] = inotify_add_watch(monitor.fd, path, *maskPtr);
    *wd[1] = inotify_add_watch(monitor.fd, path, *maskPtr);

    if(*wd[0] < 0 || *wd[1] < 0){
        perror("Sorry");
        exit(1);
    }
}

Code taken from the main function

monitor.mask[0] = ENOENT;
monitor.mask[1] = IN_CREATE;

printf("Choose the source path: ");
scanf("%s", pathname1);
monitor.wd[0] = *pathname1;
inotifyFunc(pathname1, &monitor.mask[0], monitor.wd[0]);
  • It might be just plain ignorance from me (I've never used inotify), but if `monitor` is of type `struct inotify_event`, as I suspect, `wd` is of type `int` and absolutely not an array of strings. Also, the `mask` field of the same struct is of type `uint32_t`, so I don't see why you used it as an array. Could you please post the struct definition of the type of `monitor`? – LuxGiammi Jan 03 '21 at 13:52
  • I think you chose a bad name `wd`. What does this name mean? Which more descriptive name could you use? Maybe, if you answer this question, you can fix your problem. Or, if not, you can at least clarify your original question. – anatolyg Jan 03 '21 at 13:53
  • *Never* use `"%s"` in scanf. It is no better than `gets`. If you're going to use scanf for this, at the very least use a width in the conversion specifier. eg `char path[256]; scanf("%255s", path);` – William Pursell Jan 03 '21 at 14:13

2 Answers2

1

From this scanf here

scanf("%s", pathname1);

It seems that pathname1 is a pointer to char. If that's the case, then to do that

monitor.wd[0] = *pathname1;

monitor.wd[0] has to be a char. So when you pass it to a function you should receive a simple char rather than an array of pointers to int: int *wd[2]

void inotifyFunc(char *path, uint32_t *maskPtr, char wd)

If you give more context maybe we can get what you were trying to do.

anotherOne
  • 1,513
  • 11
  • 20
  • okay but if I declared `char wd` then my array will be useless. I wrote char wd[2] but this line `inotifyFunc(pathname1, &monitor.mask[0], monitor.wd[0]);` is still giving me the same error. – Inotify Line Jan 03 '21 at 14:14
  • For now it seems that `monitor.wd[0]` is just a character. You can't pass a character and expect to receive an array of 2 characters – anotherOne Jan 03 '21 at 14:21
0
monitor.wd[0] = *pathname1;

This line makes no sense. I have no idea what you were trying to do, but whatever it is, this line is definitely wrong. inotifyFunc only uses monitor.wd for output, so there's no point in setting monitor.wd to some value before calling inotifyFunc. Furthermore monitor.wd[0] is a pointer to an integer (although maybe you intended an integer), and assigning a character to that makes no sense. And why would you take the first character of a file path anyway? So remove this line.

Given the error you're getting, it seems that monitor.wd is an array of integers:

struct {
    …
    int wd[2];
} monitor;

(But I shouldn't have to guess! Always post complete code when you ask a question.) If so, you're declaring and using it wrong in inotifyFunc: wd in inotifyFunc is an array of pointers to integers, not an array of integers. Assuming that monitor.wd is in fact an array of integers as declared above, change inotifyFunc to use it correctly by removing the spurious pointer dereferences:

void inotifyFunc(char *path, uint32_t *maskPtr, int wd[2]){
    monitor.fd = inotify_init();
    if(fcntl(monitor.fd, F_SETFL, O_NONBLOCK)){
       perror("inotify not initialized: ");
       exit(0);
    }

    wd[0] = inotify_add_watch(monitor.fd, path, *maskPtr);
    wd[1] = inotify_add_watch(monitor.fd, path, *maskPtr);

    if(wd[0] < 0 || wd[1] < 0){
        perror("Sorry");
        exit(1);
    }
}

If you really wanted to have an array of pointers to integers in inotifyFunc, then fix the declaration of monitor.wd and make sure that the pointers point to allocated memory before calling it.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254