【问题标题】:Is it possible to define a var to be a pointer to const when the type is defined as a pointer to a struct?当类型定义为指向结构的指针时,是否可以将 var 定义为指向 const 的指针?
【发布时间】:2022-11-23 22:09:56
【问题描述】:

假设test_t 定义如下:

typedef struct test_t { 
    void *unused; 
} *(test_t)

是否可以在不修改 test_t 的定义的情况下将变量定义为指向 const 的指针?

const test_t var 将是指向 struct test_t 的 const 指针,对吧?

我有这个问题,因为 sonarqube 建议“将此变量的类型设为指向常量的指针”,但我无法更改定义,因为它用于许多其他地方,变量应该是指向 struct test_t 的指针。

【问题讨论】:

标签: c pointers


【解决方案1】:

不要将指针隐藏在 typedef 后面。这是一种非常糟糕的做法,在您的情况下,无法声明此类变量。

typedef struct test_t { 
    void *unused; 
} test_t;

const test_t *var;

【讨论】:

    猜你喜欢
    • 2021-10-16
    • 2015-09-10
    • 2017-05-15
    • 2023-03-18
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    相关资源
    最近更新 更多