我在尝试在 C 中动态分配结构时遇到问题:
typedef struct
{
uint8_t wifiSSID[30];
uint8_t wifiPassword[20];
}
tWifiPair;
typedef struct
{
tWifiPair *wifiNetworks; // this needs to become an array with 2 elements
// if I do the above like this tWifiPair wifiNetworks[1] - all works fine
}
tEEPROMSettings;
tEEPROMSettings gEEPROMSettings;
int main()
{
gEEPROMSettings.wifiNetworks = (tWifiPair *)calloc(2, sizeof(tWifiPair));
// .... writing to gEEPROMSettings.wifiNetworks[0].wifiSSID crashes the program, unfortunately I can't see the error, but the compiler doesn't throw any errors/warnings
}
如果这个 tWifiPair *wifiNetworks 是静态完成的 - tWifiPair wifiNetworks[1] - 它工作正常,但我需要动态地完成它,并且可能在程序运行时更改它。
这是在嵌入式平台上运行的 - ARM tm4c1294ncpdt,编译器是 CCS6。
你能指出错误在哪里吗?谢谢!