C 编程:结构体初始化方式的差异
我有下面的代码可以工作
wifi_config_t wifi_config = {
.sta = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS,
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
.pmf_cfg = {
.capable = true,
.required = false
},
},
};
,这个失败了
wifi_config_t wifi_config;
memcpy(wifi_config.sta.ssid, ssid, strlen((const char*) ssid));
memcpy(wifi_config.sta.password, password, strlen((const char*) password));
wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK,
wifi_config.sta.pmf_cfg.capable = true;
wifi_config.sta.pmf_cfg.required = false;
但是,如果我更改为这种方式,它可以工作
wifi_config_t wifi_config = {
.sta = {
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
.pmf_cfg = {
.capable = true,
.required = false
},
},
};
memcpy(wifi_config.sta.ssid, ssid, strlen((const char*) ssid));
memcpy(wifi_config.sta.password, password, strlen((const char*) password));
使用 {...} 初始化和先声明变量,然后一一分配其字段之间有什么区别?
I have below code that works
wifi_config_t wifi_config = {
.sta = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS,
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
.pmf_cfg = {
.capable = true,
.required = false
},
},
};
And this one is failed
wifi_config_t wifi_config;
memcpy(wifi_config.sta.ssid, ssid, strlen((const char*) ssid));
memcpy(wifi_config.sta.password, password, strlen((const char*) password));
wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK,
wifi_config.sta.pmf_cfg.capable = true;
wifi_config.sta.pmf_cfg.required = false;
But, if I change to this way, it works
wifi_config_t wifi_config = {
.sta = {
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
.pmf_cfg = {
.capable = true,
.required = false
},
},
};
memcpy(wifi_config.sta.ssid, ssid, strlen((const char*) ssid));
memcpy(wifi_config.sta.password, password, strlen((const char*) password));
What is the difference between initialization using {...} and declaring the variable first, then assign its fields one by one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的第一个
memcpy
失败有两个原因:strlen
不计算字符串 nul 终止。因此,如果您的字符串是"abc"
,它只返回 3,这不足以复制完整的字符串。您需要将+1
添加到strlen
结果中。wifi_config
未初始化,这意味着您的字符串数组包含随机字符,在您的情况下,这些字符不具有可能充当 null 的零值0
。< /p>原因 2 也是您的最后一个示例按预期工作的原因。 当您使用初始值设定项时,未特别提及的字段将使用默认值进行初始化。对于整数类型数组,它们用零初始化。因此,即使您的 memcpy 无法复制 nul 终止符,也已经有零字节可以用于相同的目的。
我建议您始终至少将变量初始化为零:
Your first
memcpy
fails for 2 reasons:strlen
doesn't count string nul termination. So if your string is"abc"
, it only returns 3, which is not enough to copy complete string. You need to add+1
to yourstrlen
results.wifi_config
is not initialized, which means that your string arrays contain random characters, which in your case did not have zero value0
that could have acted as null.Reason 2 is also why your last example works as expected. When you use initializer, fields which you haven't specifically mentioned are initialized with default values. In case of integer type arrays, they are initialized with zero. So even if your
memcpy
fails to copy nul termination, there is already zero byte which will serve the same purpose.I recommend that you always at least zero initialize your variables: