0

I am working on a project called monitoring system using inotify I saw some sample code online but I didn't understand this line. inotify_event makes you notified about the event/changes in file/directory. But what *event does and why is &buffer[i] used? Could you please explain it to me? Here is the link to the sample code: https://www.thegeekstuff.com/2010/04/inotify-c-program-example/

struct inotify_event *event=(struct inotify_event*)&buffer[i];
  • Read `man inotify` – Arkadiusz Drabczyk Dec 26 '20 at 15:03
  • 1
    `struct inotify_event *event=(struct inotify_event*)&buffer[i];` is a [strict aliasing violation](https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule). You can't safely treat an array of `char` values as something it's not. The code in that link also ignores the possibility of a short read. – Andrew Henle Dec 26 '20 at 15:07
  • `struct inotify_event` is a data type used by inotify. `struct inotify_event *event` declares `event` as a pointer to an object of type `struct inotify_event`. As it appears in that declaration, `=(struct inotify_event*)&buffer[i]` is an *initializer*, specifying the initial value of `event` to be a pointer to the address of the `i`th element of array `buffer`. This may or may not constitute a strict-aliasing violation, depending on the element type of `buffer`, among other details. – John Bollinger Dec 26 '20 at 15:50

0 Answers0