简体   繁体   中英

void arithmetic in C++

I have a structure:

struct {
  Header header;
  uint32_t var1;
  uint32_t var2;
  char var3;
  char var4[4];
};

You get the hint. The thing is that I am receiving byte arrays over the network, and I first have to parse the Header first. So I first parse the header first, and then I have to parse the rest of the structure.

I tried,

void* V = data;  // which is sizeof(uint32_t) * 2 + sizeof(char) * 5

and then try to parse it like (V), V+sizeof(uint32_t) ... etc. etc.

but it gave compiler errors. How do I parse the rest of this struct over the network?

The fundamental unit of data in C++ is char . It is the smallest type that can be addressed, and it has size one by definition. Moreover, the language rules specifically allow all data to be viewed as a sequence of chars. All I/O happens in terms of sequences (or streams) of chars.

Therefore, your raw data buffer should be a char array.

(On the other hand, a void * has very specific and limited use in C++; it's main purpose is to designate an object's address in memory. For example, the result of operator new() is a void * .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM