cinitializationstructure

C programming: Differences between structure initialization way


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?


Solution

  • 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.