简体   繁体   English

当类型定义为指向结构的指针时,是否可以将 var 定义为指向 const 的指针?

[英]Is it possible to define a var to be a pointer to const when the type is defined as a pointer to a struct?

Let's say test_t is defined as follows:假设test_t定义如下:

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

Is it possible define a variable to be a pointer to const without modifying the definition of test_t ?是否可以在不修改test_t定义的情况下将变量定义为指向 const 的指针?

const test_t var would be a const pointer to struct test_t , right? const test_t var将是指向struct test_t的 const 指针,对吗?

I have this problem since sonarqube recommends to "Make the type of this variable a pointer-to-const" but I can't change the definition since it is used in many other places where the variable should be a pointer to struct test_t .我有这个问题,因为 sonarqube 建议“将此变量的类型设为指向常量的指针”,但我无法更改定义,因为它用于许多其他地方,变量应该是指向struct test_t的指针。

Do not hide pointers behind typedefs.不要将指针隐藏在 typedef 后面。 It is a very bad practice and in your case it makes impossible to declare such variable.这是一种非常糟糕的做法,在您的情况下,无法声明此类变量。

typedef struct test_t { 
    void *unused; 
} test_t;

const test_t *var;

This typedef declaration这个 typedef 声明

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

declares the name test_t as an alias for the type struct test_t * .将名称test_t声明为类型struct test_t *的别名。

So this declaration所以这个声明

const test_t var;

that may be equivalently written like可以等效地写成

test_t const var;

actually denotes the following declaration实际上表示以下声明

struct test_t * const var;

That is it declares the variable var as a constant pointer not as a pointer to a constant object of the type struct test_t .也就是说,它将变量var声明为常量指针,而不是指向struct test_t类型的常量 object 的指针。

If you want to declare the variable var as a pointer to constant data then you have to write如果要将变量var声明为指向常量数据的指针,则必须编写

const struct test_t *var;

If you want to declare a constant pointer to constant data then you have to write如果你想声明一个指向常量数据的常量指针,那么你必须写

const struct test_t * const var;

However if you want to declare a constant pointer then you need to initialize it in its declaration.但是,如果您想声明一个常量指针,则需要在其声明中对其进行初始化。

Otherwise you could rewrite the typedef declaration the following way否则你可以通过以下方式重写 typedef 声明

typedef struct test_t { 
    void *unused; 
} test_t;

and then write然后写

const test_t *var;

In this case the name test_t is an alias for the type specifier struct test_t and the qualifier const produces the type specifier const struct test_t .在这种情况下,名称test_t是类型说明符struct test_t的别名,限定符const生成类型说明符const struct test_t

Yes it is possible to do without modifications.是的,可以不做任何修改。 You just need to copy-paste the entire typedef and then change that , eg您只需要复制粘贴整个typedef然后更改,例如

typedef const struct { 
    void *unused; 
} *const_test_t;

Of course you couldn't pass this to anything that requires pointer to non-const test_t and such.当然,您不能将它传递给任何需要指向非常量test_t等的指针。

However if the declaration indeed uses struct test_t {...} then do note that it also declares the struct tag test_t , which you can use too.但是,如果声明确实使用了struct test_t {...}那么请注意它还声明了 struct 标记test_t ,您也可以使用它。 So, instead of所以,而不是

const test_t var:

you would use the struct tag with the struct keyword:您将使用带有 struct 关键字的 struct 标签:

const struct test_t *ptr;

But if the struct was declared anonymously ie typedef const struct { } *test_t;但是,如果该结构是匿名声明的,即typedef const struct { } *test_t; then copying is the only option.那么复制是唯一的选择。

The real fix is (as everyone says here) to not use a typedef to hide pointers wherever they shouldn't be.真正的解决方法是(正如这里每个人所说的那样)使用 typedef 来隐藏不应该出现的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM