简体   繁体   中英

c++, reinterpret_cast structure* to unsigned char*

At first I'm sorry for my English:) So, I have a structure and variable

typedef struct
{
  GHEADER  m_Header;
  BYTE    *m_Buf;
  Addr    *m_Abonent;
}__attribute__((packed)) PACKET;

unsigned char* uc_ptr;

I need to send to some function unsigned char pointer argument. I tried to use reinterpret_cast to cast a pointer to PACKET type to unsigned char* .

PACKET* t_PACKET;
uc_ptr = reinterpret_cast<unsigned char*>(t_PACKET);

But then I tried

std::cout << *uc_ptr << std::endl;

I don't see anything. Why? And how to cast this correctly?

When you use << to output a char you get a single character written to the output. Many characters such as \\0 do not show up on the console.

Try this instead to see what I mean:

std::cout << static_cast<unsigned int>(*uc_ptr) << std::endl;

You'll need a loop to get all of the bytes in the structure.

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