C语言出现报错 表达式必须是指向完整对象类型的指针

写下列代码时,出现了这个报错
E0852 表达式必须是指向完整对象类型的指针
是在函数最后一行那个destination++处出现的
请问这个报错是什么意思?

/*实现逆序的Memcpy方法。
函数原型: void * reversememcpy ( void * destination, const void * source, int num );
功能要求: 从source所指的内存地址的起始位置开始拷贝num个字节,按字节逆序保存到目标
            destination所指的内存地址的起始位置中。
返回值:为destination

注意:
1. 本函数实现的是按字节逆序拷贝。例如source指向位置,如果依次保存了'a','b','c','d',当num=4时,
    则逆序拷贝后destination指向的位置应该依次'd','c','b','a'.
2. 将源文件命名为functions.cpp。
3. 遇到异常情况,输出"error";否则不要随意输出,会视为错误。*/

#include
void* reversememcpy(void* destination, const void* source, int num);
int main()
{
    int num;
    char destination[10];
    char source[10] = { "abcd" };
    reversememcpy(destination,source,num);
    for (int i = 0; destination[i] != ' '; i++) printf("%c", destination[i]);
    return 0;
}
void* reversememcpy(void* destination, const void* source, int num)
{
    if (destination == NULL || source == NULL) printf("error");
    else {
        int i;
        for (i = 0; (char)destination != ' ' && (char)destination != '\0'; i++)destination++;
        
    }
}

基于Monster 组和GPT的调写:
该报错意味着正在尝试对不完整的类型进行指针运算。这通常是由于尝试使用未定义的指针引用或未分配内存所致。在代码中,出现此错误是因为在函数 reversememcpy 的最后一行中对指针 destination 进行了递增运算,但该指针尚未指向完整的对象。

要解决这个问题,需要确保 destination 指向一个完整的对象。在这种情况下,应该使用一个指向数组的指针,例如 char*,而不是 char[] 数组,以便可以对指针执行递增运算。还需要将指针初始化为有效的内存位置,例如:

char* destination = new char[10];


最后,确保在函数 reversememcpy 中完成所有的逆序拷贝操作,并将指针 destination 返回作为函数的返回值。



void* reversememcpy(void* destination, const void* source, int num)
{
    if (destination == NULL || source == NULL) {
        printf("error");
        return NULL;
    }
    else {
        char* dest = static_cast<char*>(destination);
        const char* src = static_cast<const char*>(source);
        dest += num - 1;
        for (int i = 0; i < num; i++) {
            *dest-- = *src++;
        }
        return destination;
    }
}

我还使用了 static_cast 将 void* 转换为 char* 和 const char*,以避免编译器警告。

因为第29行的destination是个 void的指针,所以把她转为char类型会报错,如果判断条件是当destination指向的位置不为空字符,可以使用

 *((char*)destination)!='\0'

然后代码的逻辑有点问题,如果是要实现题目的要求,可以使用两个个字符指针分别赋值为destination和source ,然后从source第num-1个字节逆序拷贝num个字符到destination开始位置,即可实现题目的逆序拷贝的要求。

修改如下:

参考链接:
https://zhidao.baidu.com/question/488271098203042092.html

/*实现逆序的Memcpy方法。
函数原型: void * reversememcpy ( void * destination, const void * source, int num );
功能要求: 从source所指的内存地址的起始位置开始拷贝num个字节,按字节逆序保存到目标
            destination所指的内存地址的起始位置中。
返回值:为destination
注意:
1. 本函数实现的是按字节逆序拷贝。例如source指向位置,如果依次保存了'a','b','c','d',当num=4时,
    则逆序拷贝后destination指向的位置应该依次'd','c','b','a'.
2. 将源文件命名为functions.cpp。
3. 遇到异常情况,输出"error";否则不要随意输出,会视为错误。*/
 
#include<stdio.h>
void* reversememcpy(void* destination, const void* source, int num);
int main()
{
    int num=3;  // 必须给要拷贝的字符个数设置一个数值 
    char destination[10];
    char source[10] = { "abcd" };
    reversememcpy(destination,source,num);
    for (int i = 0; i<num; i++) printf("%c", destination[i]);
    return 0;
}
void* reversememcpy(void* destination, const void* source, int num)
{
//    printf("before, source=%s,des=%s\n",source,destination);
    if (destination == NULL || source == NULL) printf("error");
    else {
        int i;
        char * s = (char*)source; //使用一个字符指针指向要拷贝字符的源地址
//        for (i = 0; i<num; i++){
//            s++;
//        }
//        printf("i=%d\n",i);


        
        int j,k=0;
        char * d =     ((char*)destination); //使用一个字符指针指向要拷贝到的目的地址 
        for(j=num-1;j>=0;j--,k++){  // 从源地址第num-1个字节开始往前拷贝num个字符到目的地址 
        //    printf("s[%d]=%c\n",j,s[j]);
            d[k]=s[j];
        }
    //    printf("k=%d\n",k);
       // d[k]='\0';
        //printf("des=%s\n",d);
    }
}
 

img