C - Dereferencing void pointer

前端 未结 5 428
独厮守ぢ
独厮守ぢ 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:34

    It's not allowed to dereference void pointer. You need to cast it to another pointer type:

    cur_a = (unsigned char *)a;
    

    Similarly, you can't assign anything to *a. Right code is:

    void ft_swap(void *a, void *b, size_t nbytes) {
        unsigned char  *cur_a = (unsigned char *) a;
        unsigned char  *cur_b = (unsigned char *) b;
    
        for (size_t i = 0; i < nbytes; ++i) {
            unsigned char tmp = cur_a[i];
            cur_a[i] = cur_b[i];
            cur_b[i] = tmp;
        }
    }
    
    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2021-01-28 16:37
        cur_a = (unsigned char *)*a + i; // here
                                 ^^^ that is dereferencing a void*
    
        cur_b = (unsigned char *)*b + i; // here
                                 ^^^ that is dereferencing a void* also.
    

    In the lines:

        *a = cur_b;
        *b = cur_a;
    

    you are dereferencing void* too.

    Here's my suggestion to fix the function:

    void ft_swap(void *a, void *b, size_t nbytes)
    {
       unsigned char* cpa;
       unsigned char* cpb;
       size_t          i;
       unsigned char c;
    
       cpa = (unsigned char *)a;
       cpb = (unsigned char *)b;
    
       for ( i = 0; i < nbytes; ++i )
       {
          c = cpa[i];
          cpa[i] = cpb[i];
          cpb[i] = c;
       }
    }
    
    0 讨论(0)
  • 2021-01-28 16:47

    Because you are dereferencing void pointers:

    void    ft_swap(void *a, void *b, size_t nbytes)
    {
     ...
            cur_a = (unsigned char *)*a + i; // here
            cur_b = (unsigned char *)*b + i; // here
    

    Doing *a means you first dereference a, and then you cast the result (whatever that is, dereferencing void* doesn't make much sense) to a pointer to unsigned char. Doing

    cur_a = *((unsigned char *)a) + i;
    

    makes more sense.

    0 讨论(0)
  • 2021-01-28 16:53

    Look your statement

    cur_a = (unsigned char *)*a + i; // here
    

    if a is a pointer to void (void *a) then *a = void. Then the subexpression (unsigned char *)*a means that you want to cast void to a pointer to char.

    But void means 'inexistent type' in C, from this the error. You may ask why a pointer to void make sense instead, it makes sense because it is an address, that is a valid data type in C. You can use the address for all the operations that doesn't involve the pointed type. I.e. assigning the address to a pointer to whichever type of data, and the reverse, is legal. It's is illegal an expression like a[2] where the subscript operator [] need the size of the data pointed to compute the address where to retrieve the value. Of course void, as bogus type, have no size (same way as it miss many other properties).

    Said that I would conclude that it was just an error in your code, and that what you really want to do is:

    void    ft_swap(void *a, void *b, size_t nbytes)
    {
        unsigned char   cur_a;    //value not pointers
        unsigned char   cur_b;
        size_t          i;
    
        i = 0;
        while (i < nbytes)
        {
            cur_a = *((unsigned char *)a + i); // here
            cur_b = *((unsigned char *)b + i); // here
    
            *a = cur_b;
            *b = cur_a;
    
            i++;
        }
    }
    
    0 讨论(0)
提交回复
热议问题