error: cannot convert 'double*' to 'char*' for argument '1' to 'void swap(char*, char*, unsigned int)'

前端 未结 3 350
悲&欢浪女
悲&欢浪女 2021-01-29 00:14
#include 

void swap( char* pA,  char* pB, unsigned tam) {
    for( unsigned i = 0; i < tam; i++) {
        char tmp = *pA;
        *pA = *pB;
                 


        
3条回答
  •  梦毁少年i
    2021-01-29 00:49

    Well, your function is expecting a pointer to a character for the first two arguments

    void swap( char* pA,  char* pB, unsigned tam) 
    

    but you are passing in pointers to double

    double a = 1.0;
    double b = 2.0;
    swap(&a, &b, sizeof(double)); //This line gives the error
    

    The following would allow you to swap two doubles, unless there is a specific reason you are swapping one byte at a time:

    void swap(double *pA, double *pB, unsigned int tam) {
        double tmp = *pA;
        *pA = *pB;
        *pB = tmp;
    }
    

提交回复
热议问题