C 编程:结构体初始化方式的差异

发布于 2025-01-09 17:46:54 字数 1095 浏览 0 评论 0原文

我有下面的代码可以工作

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无所谓啦 2025-01-16 17:46:54

您的第一个 memcpy 失败有两个原因:

  1. strlen 不计算字符串 nul 终止。因此,如果您的字符串是 "abc",它只返回 3,这不足以复制完整的字符串。您需要将 +1 添加到 strlen 结果中。

  2. wifi_config 未初始化,这意味着您的字符串数组包含随机字符,在您的情况下,这些字符不具有可能充当 null 的零值 0。< /p>

原因 2 也是您的最后一个示例按预期工作的原因。 当您使用初始值设定项时,未特别提及的字段将使用默认值进行初始化。对于整数类型数组,它们用零初始化。因此,即使您的 memcpy 无法复制 nul 终止符,也已经有零字节可以用于相同的目的。

我建议您始终至少将变量初始化为零:

wifi_config_t wifi_config = {0}; // Set all fields to 0 or NULL.

Your first memcpy fails for 2 reasons:

  1. 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 your strlen results.

  2. wifi_config is not initialized, which means that your string arrays contain random characters, which in your case did not have zero value 0 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:

wifi_config_t wifi_config = {0}; // Set all fields to 0 or NULL.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文