C 错误:无法转换';双*';至';字符*';对于参数';1';至';无效交换(字符*、字符*、无符号整数)和#x27; #包括 无效交换(字符*pA、字符*pB、未签名tam){ for(无符号i=0;i

C 错误:无法转换';双*';至';字符*';对于参数';1';至';无效交换(字符*、字符*、无符号整数)和#x27; #包括 无效交换(字符*pA、字符*pB、未签名tam){ for(无符号i=0;i,c,C,当我调用swap方法时,我在标题中得到了错误消息,你知道为什么吗?我看到许多其他帖子都有解决方案,但没有一篇文章解决了这个问题。好吧,您的函数希望前两个参数有一个指向字符的指针 #include <stdio.h> void swap( char* pA, char* pB, unsigned tam) { for( unsigned i = 0; i < tam; i++) { char tmp = *pA; *pA = *pB;

当我调用swap方法时,我在标题中得到了错误消息,你知道为什么吗?我看到许多其他帖子都有解决方案,但没有一篇文章解决了这个问题。好吧,您的函数希望前两个参数有一个指向字符的指针

#include <stdio.h>

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

        pA = pA + 1;
        pB = pB + 1;
    }
}

int main(int argc, char** argv) {
    double a = 1.0;
    double b = 2.0;

    printf("linea: %d - antes a(%f) b(%f)\n", __LINE__, a, b);

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

    printf("linea: %d - despues a(%f) b(%f)\n", __LINE__, a, b);
}
但是你在传递指向双精度的指针

void swap( char* pA,  char* pB, unsigned tam) 
以下允许您交换两个双字节,除非有特定原因,您一次交换一个字节:

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

好的,函数的前两个参数需要一个指向字符的指针

#include <stdio.h>

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

        pA = pA + 1;
        pB = pB + 1;
    }
}

int main(int argc, char** argv) {
    double a = 1.0;
    double b = 2.0;

    printf("linea: %d - antes a(%f) b(%f)\n", __LINE__, a, b);

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

    printf("linea: %d - despues a(%f) b(%f)\n", __LINE__, a, b);
}
但是你在传递指向双精度的指针

void swap( char* pA,  char* pB, unsigned tam) 
以下允许您交换两个双字节,除非有特定原因,您一次交换一个字节:

double a = 1.0;
double b = 2.0;
swap(&a, &b, sizeof(double)); //This line gives the error
我在标题中得到了错误消息?因为您要传递双变量地址并使用
char*
进行捕获,所以编译器会说

应为“char*”,但参数的类型为“double*”

因此,您有两种方法,一种是
typecast
双变量的地址为
char*
,另一种是
double*
本身的catch

案例1:

void swap(double *pA, double *pB, unsigned int tam) {
    double tmp = *pA;
    *pA = *pB;
    *pB = tmp;
}
案例2:- 第二种方法是将双变量的地址键入为
char*
发送到
swap()

void交换(char*pA,char*pB,unsigned int-tam){
for(无符号i=0;i
我在标题中看到错误消息了吗?因为您要传递双变量地址并使用
char*
进行捕获,所以编译器会说

应为“char*”,但参数的类型为“double*”

因此,您有两种方法,一种是
typecast
双变量的地址为
char*
,另一种是
double*
本身的catch

案例1:

void swap(double *pA, double *pB, unsigned int tam) {
    double tmp = *pA;
    *pA = *pB;
    *pB = tmp;
}
案例2:- 第二种方法是将双变量的地址键入为
char*
发送到
swap()

void交换(char*pA,char*pB,unsigned int-tam){
for(无符号i=0;i
编写通用交换函数有多种方法。如果它只用于一种类型(因此不需要是泛型的),则不需要size参数,您可以传递相关类型的指针(
double*
,在问题中)并使用间接方式进行交换

void swap( char *pA,  char *pB, unsigned int tam) {
        for( unsigned i = 0; i < tam; i++) {
                char tmp = *pA;
                *pA = *pB;
                *pB = tmp;
                pA = pA + 1;
                pB = pB + 1;
        }
}
int main(int argc, char** argv) {
        double a = 1.0;
        double b = 2.0;
        printf("linea: %d - antes a(%f) b(%f)\n", __LINE__, a, b);
        swap((char*)&a, (char*)&b, sizeof(double)); //typecast &a to (char*)&a
        printf("linea: %d - despues a(%f) b(%f)\n", __LINE__, a, b);
        return 0;
}
将其转化为:

extern void double_swap(double *d1, double *d2);

void double_swap(double *d1, double *d2)
{
    double d = *d1;
    *d1 = *d2;
    *d2 = d;
}
可以将其放置在收割台中并安全使用

如果它将用于多种类型,则应在函数参数中使用
void*
(对于类型的大小,应使用
size\t

尽管GCC允许,但C标准规定不能增加a
void*
,因为没有已知的大小来增加它。这就是指针被转换为
无符号字符*
的原因。显然,您可以调整块大小以适合您的系统。16..1024范围内的任何2次方都可能可用,如果愿意,可以使用除2次方以外的其他值

如果不介意开销,可以动态分配缓冲区:

enum { CHUNK_SIZE = 64 };

static inline size_t min_size(size_t x, size_t y) { return (x < y) ? x : y; }

/* Implementation 2: Using a fixed size buffer */
void generic_swap(void *v1, void *v2, size_t size)
{
    unsigned char sp[CHUNK_SIZE];
    unsigned char *p1 = v1;
    unsigned char *p2 = v2;
    size_t chunk;
    while ((chunk = min_size(size, CHUNK_SIZE)) != 0)
    {
        memmove(sp, p1, chunk);
        memmove(p1, p2, chunk);
        memmove(p2, sp, chunk);
        p1 += chunk;
        p2 += chunk;
        size -= chunk;
    }
}
如果内存分配失败,则不会进行交换。这很糟糕,所以您可能会使用“固定大小的缓冲区,并在块中交换”,但无论如何,这可能比这更快

我会优先使用实现2而不是实现3;动态内存分配是昂贵的。我可能会优先使用实现2而不是实现1,因为循环的额外成本是最小的,并且使用固定数量的堆栈效果很好。我没有做过任何基准测试 这些都是为了验证我的断言。(如果您要交换兆字节大小的数据块,您可能应该三思而后行——改用指针。如果您只交换较小的数据块,则实现1简单而安全。)

使用任何通用交换实现,您的主程序将变成:

/* Implentation 3: Using dynamic memory allocation */
void generic_swap(void *v1, void *v2, size_t size)
{
    char *sp = malloc(size);
    if (sp != 0)
    {
        memmove(sp, v1, size);
        memmove(v1, v2, size);
        memmove(v2, sp, size);
        free(sp);
    }
}
我假设在
main()
开始之前至少有
generic\u swap()的声明可用

请注意,使用
double\u swap()
generic\u swap()
表示代码中不需要强制转换。减少演员阵容是个好主意™.


另请参见。

编写通用交换函数有多种方法。如果它只用于一种类型(因此不需要是泛型),则不需要size参数,您可以传递相关类型的指针