GCC 警告:类型为 void * 的指针算术
GCC Warning: pointer arithmetic with type void *
我必须在 Linux 上用 C 编写一个函数来读取或写入通用数据。
我可以读取(或写入)大数据,所以我使用读取的字节数做了一段时间。
例如,在下一次调用 i 时,读取原始指针 + 我读取了多少字节。但我不知道类型所以我使用了 void * 但 gcc 说:
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);
我可以这样做吗?我应该忽略这个警告吗?
将 void *
转换为 char *
。这样,您就有了一个大小为 1 的基础类型来进行指针运算。
(char*)data + (s*type)
我必须在 Linux 上用 C 编写一个函数来读取或写入通用数据。
我可以读取(或写入)大数据,所以我使用读取的字节数做了一段时间。 例如,在下一次调用 i 时,读取原始指针 + 我读取了多少字节。但我不知道类型所以我使用了 void * 但 gcc 说:
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);
我可以这样做吗?我应该忽略这个警告吗?
将 void *
转换为 char *
。这样,您就有了一个大小为 1 的基础类型来进行指针运算。
(char*)data + (s*type)