struct结构体和char型数组的相互转化
生活随笔
收集整理的這篇文章主要介紹了
struct结构体和char型数组的相互转化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
針對內存連續的結構體,把一整段內存拷貝到char數組對應的內存中。考慮4字節對齊!
struct Test {int a;char b;char c[1024];unsigned short d; };char *ch = "abcdefghijklmn1234567890"; char *chTest = new char[2048]; memset(chTest, 0, sizeof(chTest));Test *t1 = new Test; t1.a = 10; t1.b = -20; strcpy(t1.c, ch); t1.d = 12;//結構體轉為char * memcpy(chTest, (char *)t1, sizeof(Test));//char *轉為結構體 Test *t2; t2 = (Test *)chTest;?
總結
以上是生活随笔為你收集整理的struct结构体和char型数组的相互转化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 结构体自动化转为char数组的实现
- 下一篇: 由浅入深剖析go channel