首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >指向常量结构的常量指针为零

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

Stack Overflow用户
提问于 2020-04-21 19:55:56
回答 1查看 55关注 0票数 0

我正在开发别人开发的一段代码,我试图使结构成为静态的。但在调试时,指针似乎是零(而不是指向一个为零的变量):

代码语言:javascript
复制
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处理器开发的。

EN

回答 1

Stack Overflow用户

发布于 2020-04-21 20:31:11

这不应该发生,如果我复制了你的完全相同的代码,它看起来就像我所期望的那样工作。不幸的是,您没有包括如何显示指针值。

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

代码语言:javascript
复制
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。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61342958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档