我正在开发别人开发的一段代码,我试图使结构成为静态的。但在调试时,指针似乎是零(而不是指向一个为零的变量):
typedef struct WiFiIP{
// Keep the following in order! (pri,sec)
uint16_t WiFiPrimaryPort;
uint16_t WiFiSecondaryPort;
// Keep the following in order! (pri,sec,sntp)
char WiFiPrimaryAddress[32];
char WiFiSecondaryIpAddress[32];
char WiFiSntpIpAddress[32];
};
static const struct WiFiIP WiFiIPs = {7781,7781,"10.0.0.1","",""};
static const __typeof__ (WiFiIPs.WiFiPrimaryPort)
* const PortPtr = &WiFiIPs.WiFiPrimaryPort;
static const __typeof__(WiFiIPs.WiFiPrimaryAddress)
*AddressPtr = &WiFiIPs.WiFiPrimaryAddress;所以AddressPtr和PortPtr都是0x0。
我知道有许多其他方法可以在代码中‘硬编码’参数,但在这种情况下,现有代码需要一个指向结构的指针,我希望避免重构。
请温文点,我从来不擅长使用指针,我知道有很多关于指针的文献。我已经搜索并遵循了许多示例,但无法使其正常工作。
这段代码是为cortex m3处理器开发的。
发布于 2020-04-21 20:31:11
这不应该发生,如果我复制了你的完全相同的代码,它看起来就像我所期望的那样工作。不幸的是,您没有包括如何显示指针值。
要正确打印指针的值,可以使用如下命令:
printf("PortPtr points to memory location at: %#lX\n", (uintptr_t)PortPtr);
printf("AddressPtr points to memory location at: %#lX\n", (uintptr_t)AddressPtr);在这种情况下,您应该能够验证指针是否未初始化为NULL。
https://stackoverflow.com/questions/61342958
复制相似问题