-2
void* SendMsg(void* msg) {
    char *result;
    char tmpBuf[1000]={0};
    message send_msg =*(message*)msg;//?

The problem is * and (*)type.

(message)"parameterName";

"message" is custom made struct. I know "parameterName*" is pointer, receives address. but "*(parameter)" phrase gets value from address. How I should call this?

  • Leave a space between `=` and `*` in memoriam for the pre-historic C operators =*, =+, etcetera. – Joop Eggen Oct 28 '21 at 07:32
  • I would say `declare a new variable called send_msg of type message and initialize it with msg, cast to message star dereferenced`. – mch Oct 28 '21 at 07:33
  • In data streams (file or net), data is often serialized as bytes (for which `char` was used in C). You then send the type-erased pointer (`void*`) and the length. To convert such a pointer back to the original type here, it's first cast to a pointer of that type `message*` and then dereferenced, using the `*`-operator. – JHBonarius Oct 28 '21 at 07:42
  • `(message*)msg` is a cast from type `void*` to type `message*` (pointer to message). Then: `*` is access operator - it gives you the access to the value pointed by a pointer. Take a look also [here](https://stackoverflow.com/questions/10061332/c-asterisks-ampersand-and-star) – pptaszni Oct 28 '21 at 07:54
  • so this is equalto "*message (parameter) " Am I right? – I am not englishman Oct 28 '21 at 09:58

2 Answers2

1

The code is in C-style whwere a void pointer is passed to the function. To use it it is cast in C-style to message* and immediately dereferenced (in this case assigned to a local variable named send_msg).

This code in C++ is technically known as a red herring for possible issues.

In C++, I would expect a base-class (interface) as parameter or a template parameter.

stefaanv
  • 14,072
  • 2
  • 31
  • 53
  • so , this is same meaning to "*message Parameter". Am I right? – I am not englishman Oct 28 '21 at 10:03
  • Yes, if the parameter was passed as `message* param`, then `message send_msg = *param;` would be the same. Of course, with the code in the question things could go wrong if `void*` doesn't really point to a "message" struct. – stefaanv Oct 28 '21 at 12:16
1

(message*)msg says "pretend that msg is a pointer to an object of type message. message send_msg = *(message*)msg; says "pretend that msg is a pointer to an object of type message and copy the message object that it points at into send_msg. That will work fine if msg in fact points at an object of type message.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165