0
Follow
0
View

C language error expression must be a pointer to the full object type

luoweidong79 注册会员
2023-02-25 23:10

You have a lot of problems here, modify as follows:

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

feng1548 注册会员
2023-02-25 23:10

Write based on Monster group and GPT:
Error means that pointer manipulation is being attempted on incomplete types. This is usually due to attempts to use undefined pointer references or unallocated memory. In the code, this error occurs because the pointer destination is incremented in the last line of the function reversememcpy, but the pointer does not yet point to a complete object.

To solve this problem, make sure the destination points to a complete object. In this case, you should use a pointer to an array, such as char*, instead of a char[] array, so that you can increment the pointer. You also need to initialize the pointer to a valid memory location, such as:

char* destination = new char[10];


Finally, make sure to do all reverse copying in the function reversememcpy and return the pointer destination as the return value of the function.



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;
    }
}

I also used static_cast to convert void* to char* and const char* to avoid compiler warnings.