C - Dereferencing void pointer

前端 未结 5 575
独厮守ぢ
独厮守ぢ 2021-01-28 15:51

I\'m trying to create my own swap function but I have troubles.
Why I\'m getting \" dereferencing void pointer \" ?

void    ft_swap(void *a, void *b, size         


        
5条回答
  •  野性不改
    2021-01-28 16:36

    You want to cast the (abstract) void* pointer to a pointer to  unsigned char so code:

    cur_a = (unsigned char *)a + i;
    

    Your code was understood as cur_a = (unsigned char *)(*a) + i; which wrongly dereferences a void* abstract pointer.

    BTW, your *a = cur_b; does not make sense neither. Perhaps you want

    ((unsigned char*)a)[i] = cur_b;
    

提交回复
热议问题