Can I do arithmetic on void * pointers in C?

后端 未结 6 563
攒了一身酷
攒了一身酷 2020-12-03 14:45

is this valid

void *p = &X; /* some thing */
p += 12;

and if so what does p now point to? I have (third party) code that does this (an

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-03 15:08

    I don't think you can, because it doesn't know its type, therefore can not seek the correct amount of bytes.

    Cast it to a type first, i.e. (int).

提交回复
热议问题