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
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).
answered Jan 13, 2023 at 2:19
-
But if they are the same, why is it complaining?user88434– user884342023年01月13日 02:22:12 +00:00Commented 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);
.the busybee– the busybee2023年01月13日 07:54:19 +00:00Commented Jan 13, 2023 at 7:54
lang-cpp