What's the correct way to add 1 byte to a pointer in C/C++?

前端 未结 6 523
忘了有多久
忘了有多久 2021-01-18 08:53

I\'m using this code to move pointer by 1 byte now, but I\'m feeling something unclear..

int* a = (int*)malloc(sizeof(int));
void* b = ((char*)a)+1;   
         


        
6条回答
  •  无人共我
    2021-01-18 09:21

    ((char*)a)++

    This is one of those evil Microsoft extensions. A pointer casting expression is an rvalue, but according to the C++ language rules, the increment operator only works on lvalues. g++ refuses to compile this.

提交回复
热议问题