How to convert struct to char array in C

前端 未结 10 562
孤城傲影
孤城傲影 2020-12-19 03:00

I\'m trying to convert a struct to a char array to send over the network. However, I get some weird output from the char array when I do.

#include 

        
10条回答
  •  余生分开走
    2020-12-19 03:20

    Treating your struct as if it were a char array is undefined behavior. To send it over the network, use proper serialization instead. It's a pain in C++ and even more so in C, but it's the only way your app will work independently of the machines reading and writing.

    http://en.wikipedia.org/wiki/Serialization#C

提交回复
热议问题