給char陣列或者結構體賦值

2021-07-11 07:24:07 字數 1105 閱讀 2121

首先,定乙個乙個結構體

struct

controlwindowmsg;

定義乙個char型別字元陣列,並且賦值

char

buf[

4] = ;

buf[

0] = 1&

0xff;

buf[

1] = 0&

0xff;

buf[

2] = 3&

0xff;

buf[

3] =

104&

0xff;

然後,將buf中的內容賦值給結構體

struct

controlwindowmsg

windowmsg;

memset

(&windowmsg, 0,

sizeof

(windowmsg)); //結構體賦值為0

memcpy

(&windowmsg, buf,

sizeof

(windowmsg)); //結構體賦值

通過列印可以發現,windowmsg賦值成功

ps:順便新增一點

nstimeinterval

timeinerval = [[

nsdate

date

] timeintervalsince1970];

inttaskid = [[

nsstring

stringwithformat

:@"%f"

,timeinerval]

intvalue];

buf[

2] = taskid &

0xff;

buf[

3] = (taskid >>

8) &

0xff;

buf[

4] = (taskid >>

16) &

0xff;

buf[

5] = (taskid >>

24) &

0xff;

如果taksid是佔四個位元組的,給buf陣列賦值的時候這樣賦值

陣列直接賦值給結構體

經過幾天的努力 終於把陣列 賦值給結構體,pragma pack push 儲存原對齊並設定新的位元組對齊 pragma pack 1 typedef struct tnettopologyrecord nettopologyrecord,pnettopologyrecord pragma pack...

用Qstring給char 陣列賦值

tree data.desc desc是char 80 型別的資料 qstring newdescstr strcpy tree data.desc newdescstr.tolocal8bit data 注意 tolocal8bit 為了讓qstring變成qbytearray,也為了漢字本地化處...

物件賦值給結構體

package main import fmt log net sync time const address localhost 502 方法的巢狀!func main tcptransporter conn closetimer lastactivity 結構體 tcpclienthandler...