Swapping objects using pointers

后端 未结 10 1338
轻奢々
轻奢々 2020-12-03 18:20

I\'m trying to swap objects for a homework problem that uses void pointers to swap objects. The declaration of my function has to be:

void swap(void *a, voi         


        
10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 18:48

    First of all, note that any changes to the pointers inside the function won't be propagated to outside the function. So you're going to have to move memory around.

    The easiest way to do that is with memcpy - allocate a buffer on the stack, memcpy the appropriate size from a into it, memcpy from b to a, and one last memcpy from temp into b.

提交回复
热议问题