1

I have to write a function, in C on Linux, to read or write generic data.

I can read (or write) big data so i made a while using how many bytes I read. At the next call i, for example, read in the original pointer + how many bytes I read. But I don't know the type so I used a void * but gcc says:

membox.c: In function ‘myRW’:
membox.c:301:22: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]
w = read(fd, data + (s*type) , len - s);
                  ^
membox.c:308:23: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]
w = write(fd, data + (s*type) , len - s);

Can i do this? I should ignore this warnings?

SimC
  • 105
  • 2
  • 8

1 Answers1

6

Cast the void * to char *. That way, you have an underlying type of size 1 to do pointer arithmetic on.

(char*)data + (s*type)
dbush
  • 205,898
  • 23
  • 218
  • 273