【问题标题】:GCC Warning: pointer arithmetic with type void * [duplicate]GCC警告:类型为void *的指针算术[重复]
【发布时间】:2016-09-25 22:59:19
【问题描述】:

我必须在 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);

我可以这样做吗?我应该忽略这些警告吗?

【问题讨论】:

  • char* 可能是您想要进行算术运算的指针类型。
  • OT:那些long ints 也应该是size_ts。
  • OT^2:按照约定返回错误-1

标签: c linux gcc void-pointers pointer-arithmetic


【解决方案1】:

void * 转换为char *。这样,你就有了一个大小为 1 的底层类型来进行指针运算。

(char*)data + (s*type)

【讨论】:

    猜你喜欢
    • 2015-01-01
    • 2011-10-30
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    相关资源
    最近更新 更多