Cast pointers in C ++

I have a serial port returning uint8 as follows

uint8 bucket[255]; res = COM.com_read((char *)&bucket); 

how can I pass the bucket pointer to the buff pointer in the function below:

 ssize_t send(int s, const void *buf, size_t len, int flags); 
+4
source share
3 answers

& not required in com_read , and you should write this:

 //after removing '&' //bucket being an array converts to pointer automatically res = COM.com_read((char *)bucket); //Dont use & 

Or even better casting would be static_cast :

 res = COM.com_read(static_cast<char*>(bucket)); //C++ Style cast! 

And when sending the bucket to send you don’t have to drop it. This is implied by the compiler, because the target type is void* , and any pointer can implicitly convert to void* .

+4
source

There is no need for casting, this is done implicitly (any pointer can be placed in const void* implicitly).

Note that with explicit casting, you should prefer C ++ - casting operators:

 res = COM.com_read(static_cast<char *>(bucket)); 
+2
source

Static_cast will do the trick to and from void *, but are you sure you need an explicit cast? I think this should be done implicitly for you.

 static_cast<void *>(bucket) 
+1
source

Source: https://habr.com/ru/post/1381140/


All Articles