预选赛被丢弃在结构指针上

发布于 2025-02-09 16:50:09 字数 418 浏览 2 评论 0原文

当我声明

typedef struct myStruct
{
    //Some attributes
}myStruct_t, *pMyStruct_t;
void func(myStruct_t* someStruct);

一个结构指针如下并将其传递给功能时,我的结构和函数如下所示,一切都很好,

volatile pMyStruct_t pStruct;

但是当我以这种方式声明指针时,我会发现挥发性预选赛在功能中丢弃的错误,

volatile myStruct_t* pStruct;

我猜我问题是第一个方法是如何工作的,没有提出预选错误错误?声明psstruct变量的两种方式有什么区别?

I have struct and function declared as follows

typedef struct myStruct
{
    //Some attributes
}myStruct_t, *pMyStruct_t;
void func(myStruct_t* someStruct);

When I declare a struct pointer as follows and pass it to the function, all is fine

volatile pMyStruct_t pStruct;

But when I declare the pointer this way I get an error that the volatile qualifier is discarded in the function

volatile myStruct_t* pStruct;

I guess my question is how come the first method works and no qualifier errors are raised? What is the difference between the two ways the pStruct variable is declared?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野の 2025-02-16 16:50:10
volatile pMyStruct_t pStruct;

此代码等同于此:

myStruct_t* volatile pStruct;

与此相差有所不同:

volatile myStruct_t* pStruct;

其中的差异,哪个变量将是波动的。
在第一种情况下,指向结构本身的指针将是挥发性的。
在第二种情况下,结构对象将是挥发性的,而不是指针。
在我看来,第二选择是您的。无论如何,使指针波动几乎没有意义。

您可以用挥发性关键字指针:

typedef volatile struct myStruct
{
    //Some attributes
}myStruct_t, *pMyStruct_t;

但是在这种情况下,struct将始终是挥发性的,我不知道这对您来说是可以接受的。

volatile pMyStruct_t pStruct;

This code equivalent to this:

myStruct_t* volatile pStruct;

There is a difference with this:

volatile myStruct_t* pStruct;

Difference in that, which variable will be volatile.
In first case, pointer to the struct itself will be volatile.
In second case, struct object will be volatile, not pointer.
It seems to me, that second choice is yours. Any way, there is little sense to make pointer volatile.

You can typedef pointer with the volatile keyword:

typedef volatile struct myStruct
{
    //Some attributes
}myStruct_t, *pMyStruct_t;

But in this case, struct will always be volatile, I don't know is this acceptable for you.

心房敞 2025-02-16 16:50:09

这些声明

volatile pMyStruct_t pStruct;

volatile myStruct_t* pStruct;

不同的。

第一个表示以下

myStruct_t* volatile  pStruct;

是第一个声明中的以下内容,是指指针本身是挥发性的,而在第二个声明中,指针本身并不挥发。

您可以使用挥发性指针的值初始化功能参数。但是您可能不会丢弃尖头对象的预选赛

These declarations

volatile pMyStruct_t pStruct;

and

volatile myStruct_t* pStruct;

are different.

The first one means the following

myStruct_t* volatile  pStruct;

That is in the first declaration it is the pointer itself that is volatile while in the second declaration the pointer itself is not volatile.

You may initialize the function parameter with a value of a pointer that is itself is volatile. But you may not discard the qualifier for the pointed object

┾廆蒐ゝ 2025-02-16 16:50:09

第一个

volatile pMyStruct_t pStruct;

创建volatile变量ps struct通过复制传递给您的函数。
指向的内存不是挥发性

第二个

volatile myStruct_t* pStruct;

定义指向volatile内存的指针。

这些非常不同。

这也表明了为什么将指针类型隐藏到typedef中是一个可怕的想法。

The first one

volatile pMyStruct_t pStruct;

Creates a volatile variable pstruct that is passed by copy to your function.
The memory where it points to is not volatile.

The second one

volatile myStruct_t* pStruct;

defines a pointer to a volatile memory.

These are very different.

This also shows why it is widely regarded to be a terrible idea to hide a pointer type into a typedef.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文