I have the following code. It's a memcmp implementation for the Renesas R5F Embedded Chip family and I'm trying to test it.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <assert.h>

typedef char SInt8;
typedef unsigned char UInt8;




SInt8 one_net_memcmp(const void *vp1, const void *vp2, size_t n)
{
    UInt8 * up1;
    UInt8 * up2;
    // If invalid, just return zero since there is no error recovery
    if (!vp1 || !vp2) {
        return 0;
    }
    for (up1 = vp1, up2 = vp2; n > 0; ++up1, ++up2, --n) {
        if (*up1 != *up2) {
            return ((*up1 < *up2) ? -1 : 1);
        }
    }
    return 0;
}


void test()
{
    int i;
    UInt8 arr1[10];
    UInt8 arr2[10];

    for(i = 0; i < 10; i++)
    {
        arr1[i] = i;
        arr2[i] = i;
    }

    assert(one_net_memcmp(arr1, arr2, 10) == 0);
    arr1[3] = 1;
    assert(one_net_memcmp(arr1, arr2, 10) < 0);
    arr1[3] = 5;
    assert(one_net_memcmp(arr1, arr2, 10) > 0);
}


int main()
{
    test();

    printf("Test Passed\n");
    return 0;
}

2 Identical Warnings on line 23...

23|warning: assignment discards qualifiers from pointer target type
23|warning: assignment discards qualifiers from pointer target type
||=== Build finished: 0 errors, 2 warnings ===|

Delete the "const" and they go way. I'm not seeing anywhere where I adjust the values of vp1 or vp2, so I'm confused by this error. I've been staring at it for quite a while.

Compile it using C++ and you don't get warnings, you get errors.

23|error: invalid conversion from 'const void*' to 'UInt8*
23|error: invalid conversion from 'const void*' to 'UInt8*
||=== Build finished: 2 errors, 0 warnings ===|

Seems to compile just fine using the C compiler from Renesas.

Like I said, I've been staring at this for a while and it seems fine to me, but I may be having a brain fart. I'm using Code Blocks, so I'm sure the compiler's right. Can anyone enlighten me?

Recommended Answers

All 3 Replies

OK, added a couple more consts. Compiles with warnings using C compiler in Code Blocks. C++ still doesn't like it...

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <assert.h>

typedef char SInt8;
typedef unsigned char UInt8;




SInt8 one_net_memcmp(const void* const vp1 , const void * const vp2, size_t n)
{
    const UInt8 * up1;
    const UInt8 * up2;
    // If invalid, just return zero since there is no error recovery
    if (!vp1 || !vp2) {
        return 0;
    }

    up1 = vp1;
    up2 = vp2;

    for (up1 = vp1, up2 = vp2; n > 0; ++up1, ++up2, --n) {
        if (*up1 != *up2) {
            return ((*up1 < *up2) ? -1 : 1);
        }
    }
    return 0;
}


void test()
{
    int i;
    UInt8 arr1[10];
    UInt8 arr2[10];

    for(i = 0; i < 10; i++)
    {
        arr1[i] = i;
        arr2[i] = i;
    }

    assert(one_net_memcmp(arr1, arr2, 10) == 0);
    arr1[3] = 1;
    assert(one_net_memcmp(arr1, arr2, 10) < 0);
    arr1[3] = 5;
    assert(one_net_memcmp(arr1, arr2, 10) > 0);
}


int main()
{
    test();

    printf("Test Passed\n");
    return 0;
}
24|error: invalid conversion from 'const void*' to 'const UInt8*'
25|error: invalid conversion from 'const void*' to 'const UInt8*'
27|error: invalid conversion from 'const void*' to 'const UInt8*'
27|error: invalid conversion from 'const void*' to 'const UInt8*'

C++ requires a cast for conversions to and from void*:

SInt8 one_net_memcmp(const void* const vp1 , const void * const vp2, size_t n)
{
    const UInt8 * up1;
    const UInt8 * up2;

    // If invalid, just return zero since there is no error recovery
    if (!vp1 || !vp2) {
        return 0;
    }

    up1 = (const UInt8*)vp1;
    up2 = (const UInt8*)vp2;

    for (; n > 0; ++up1, ++up2, --n) {
        if (*up1 != *up2) {
            return ((*up1 < *up2) ? -1 : 1);
        }
    }
    return 0;
}

Cool. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.