指向常量结构的常量指针为零

时间:2020-04-21 11:55:56

标签: c pointers struct constants cortex-m3

我正在研究别人开发的一段代码,并且试图使结构静态。但是在调试时,指针似乎为零(没有指向变量为零):

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;

因此AddressPtrPortPtr均为0x0

我知道在代码中还有很多其他方法可以对参数进行“硬编码”,但是在这种情况下,现有代码希望使用指向结构的指针,所以我想避免重构。

请保持温柔,我从来都不擅长使用指针,而且我知道关于指针的文献很多。我搜索并遵循了许多示例,但无法使其正常工作。

此代码正在为cortex-m3处理器开发

1 个答案:

答案 0 :(得分:0)

这不应该发生,并且如果我复制您的确切代码,它似乎可以达到我的预期。不幸的是,您没有包括如何显示指针值。

要正确打印指针的值,可以使用以下命令:

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。