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

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

    plz, use void*

    int g = 10;
    int *a = &g;
    printf("a : %p\n",a);
    printf("a : %p\n", ++a);
    printf("a : %p\n", (void*)((char*)a+1));
    

    a : 0xbfae35dc a : 0xbfae35e0 a : 0xbfae35e1

提交回复
热议问题