【问题标题】:initialize structure array and its members to zero using memset使用 memset 将结构体数组及其成员初始化为零
【发布时间】:2017-05-24 05:18:58
【问题描述】:

我已经创建了结构数组及其成员,例如,

struct hostDB {
    u_int8_t        host_id;
    u_int8_t        host_mac[ETHER_ADDR_LEN];
    unsigned char   mdvalue[1024];
    unsigned char   password[50];
};
struct hostDB structhostdb[1024];

我需要使结构数组及其成员初始化为零。我试过这个命令,但它不起作用, memset(structhostdb,0,1024); & memset(structhostdb,0,sizeof(structhostdb)); 请帮帮我。提前致谢。

【问题讨论】:

    标签: arrays struct memset


    【解决方案1】:

    他们是一个小问题,应该是这样的:-

    memset(structhostdb,0,sizeof(struct hostDB)*1024);
    

    sizeof-> 函数需要的是结构的类型而不是结构的对象,所以它不会是“structhostdb”而是“struct hostDB”。

    为避免混淆,为结构定义和结构声明赋予不同的名称。

    【讨论】:

      猜你喜欢
      • 2011-03-24
      • 2011-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多