Search code examples
c++pipefork

c++ write()/read() buffer type not working


I am writing a project using forks and pipes in c++ in Visual Studio Code and when i write the code

int temptotal;
write(fds[1],temptotal,sizeof(int));

or

read(fds[0],temptotal,sizeof(int));

I get red underlines on the temptotal variable, saying: 'argument of type "int" is incompatible with parameter of type "const void *"'

I can provide more details on the project if necessary, but I believe it is only this individual aspect that is having issues, as everything else works as expected, but I am unable to read() and write() between child and parent. I assumed that const void * would allow me to pass any type, but I am clearly misunderstanding something. I am using Visual Studio Code using the g++ compiler.


Solution

  • You need to provide a pointer to the memory where write should write and read should read:

    write(fds[1], &temptotal, sizeof temptotal);
    //            ^
    read(fds[0], &temptotal, sizeof temptotal);
    //           ^