1

I have written this code:

char message[4];
memcpy(message, &delta, 4);

Wire.write(message, 4);

When I try to compile I get this warning:

warning: invalid conversion from ‘char*’ to ‘const uint8_t* {aka const unsigned char*}’ [-fpermissive]
   Wire.write(message, 4);
                        ^

Also followed by this note:

In file included from main.ino:5:0:
/usr/share/arduino/libraries/Wire/Wire.h:61:20: note:   initializing argument 1 of ‘virtual size_t TwoWire::write(const uint8_t*, size_t)’
     virtual size_t write(const uint8_t *, size_t);
                    ^

I do not understand what is wrong?

1 Answer 1

1

Try

Wire.write((const uint8_t*) message, 4);

I.e., cast it to the correct type, uint8_t and char are the same (at least on Arduino and for this example, as you do not char about unsigned/signed values).

(see also the comment of the busybee below, for using the cleaner C++ cast).

2
  • But if they are the same, why is it complaining?
    – user88434
    Commented Jan 13, 2023 at 2:22
  • 1
    @user88434 Because C++ is a language with a kind of stronger type system, and by purpose the warning is enabled during compilation. You as the programmer need to make clear that you change the interpretation of the bytes. -- BTW, the clean solution would not use a C cast, but a C++ cast, for example Wire.write(reinterpret_cast<uint8_t*>(message), 4);. Commented Jan 13, 2023 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.