Well ... first, unless you have a very valid reason, never use those jurrassic C-Style arrays in C++.
The type std::array exist for a reason, and is more safe, less error prone and don't decay to troublemaking raw pointers.
Concerning your issue:
Did you try static_cast ?
Zoppo
Yes, this error is correct, the buf passed to the function is a const char *, in words this means it's a pointer to const char, so the compiler has to throw an error in case the type is (implicit) changed to a none-const type.
This is good and correct, because when I call a function and pass a string to it I usually expect the string stays unchanged. Further there are cases where changing such a const char can lead to crash, i.e. when such a pointer is initialized using a string literal, i.e.:
const char* p = "hello world.";char* p1 = (char*)p; // cast the 'const' awayp1[0] = 'H'
I.e. compiled with VisualStudio this crashs due to an access violation because the compiler puts those string literals into memory segments which are read-only.
This even demonstrates that just casting away the const is not a good idea.
A type-safe solution would be to remove the const from the function declaration:
void process_send(SOCKET s, char* buf, int *len, int flags) { ... }
Of course this will cause compile errors in cases where this function is called with a const char *, you have to fix these calls in another way, ensuring no real const texts are passed.
Const-ness is an important aspect in C/C++ and should always be considered.
Hope that helps,
ZOPPO
magdiel linhares
ASKER
Hey Zoppo, i changed my cod to
void process_send(SOCKET s, char* buf, int *len, int flags) {
But beware that if you persist with C-style arrays, you're running yourself into troubles.
Zoppo
>> How can I convert from char* to BYTE*?
You can simply cast is, i.e.:
char* text = strdup( "Hello world." );
BYTE* data = (BYTE*)text;
BTW: Just for interest I'd like to ask if there is any reason why you don't use any C++ functionality? Except the leading #include <cdstdio> everything seems to be pure C. Therefor I wrote the code I posted above in the same way, but the complete problem could probably be solved better/nicer/safer when using standard C++ functionalities and STL.
Best regards,
ZOPPO
sarabande
never use those jurrassic C-Style arrays in C++.
that is not true in general. for example there is no better way in c++ to initialize arrays with plain data like the crypto tables but to use c arrays.
and if those arrays are constant there is no reason to convert to std::array or std::vector.
if you need to change values you can combine both:
but the complete problem could probably be solved better/nicer/safer when using standard C++ functionalities and STL.
the sample code is too short to decide whether c++ classes and STL give advantage. for example socket programming is highly made in c and using c++ types would require mcuh more efforts to convert from c++ to c types and reverse as if all is done with c types. that must not mean that you would use the ansi c compiler but simply use c++ where it fits better and use c where you have to provide data for a c interface.
Sara
sarabande
void process_send(SOCKET s, char* buf, int *len, int flags) {
How can I convert from char* to BYTE*?
as Zoppo showed in his sample code you simply could cast the char* to BYTE* because char size is 1 byte same as BYTE which is typedef'd as
i. e. a BYTE simply is an unsigned char what is an 8-bit unsigned integer (range 0 to 255) while char can be used as a signed 8-bit integer (if not used for text) where the highest bit (bit 7) is used as sign bit and the range is from -128 to +127.
void process_send(SOCKET s, char* buf, int *len, int flags) { BYTE * pbuf = (BYTE*)buf; // now pbuf can be used wherever a writeable BYTE array was required // alternatively use buf if a char* or const char* is required // not 'len' should be the size of the buffer and not the length of any text within the buffer ...
The type std::array exist for a reason, and is more safe, less error prone and don't decay to troublemaking raw pointers.
Concerning your issue:
Did you try static_cast ?