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

前端 未结 6 519
忘了有多久
忘了有多久 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:28

    ((char*&)a)++;
    

    Or:

    a = (int*)((char*)a+1);
    

    I hope you know exactly what you're doing. For one thing, you're ending up with - by definition - unaligned int pointer. Depending on architecture and OS, this might be trouble.

提交回复
热议问题