*(int *)foo VS (int *)*foo。这两者有什么区别?

发布于 2025-01-10 12:49:11 字数 206 浏览 0 评论 0原文

我正在开发 RTOS 项目,我试图将类型转换为 void 指针的结构类型传递给线程函数,并使用类型转换将该 void 指针取消引用为相同的结构类型。当我尝试以 (eUartDriver_t*) *args 的方式执行此操作时,出现错误。然后在互联网上找到使用 *(eUartDriver_t*) args ,但它没有解释其中的区别以及为什么它有效

I am working on RTOS project and I'm trying to pass struct type typecasted to void pointer to thread function and derefernce that void pointer using typecast to same struct type. I was getting error when trying to do it this way (eUartDriver_t*) *args. Then found on the internet to use *(eUartDriver_t*) args , but it didn't explained the difference and why does it work

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

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

发布评论

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

评论(2

千鲤 2025-01-17 12:49:11

据推测,args 被声明为 void *。表达式 *args 的意思是“args 指向的东西”,因此 *args 将是一个 void,但是void 不是可用的类型。所以 *args 是错误的代码,编译器会抱怨。

(eUartDriver_t *) args 表示“将 args 的值转换为 eUartDriver_t *”。该类型是指向eUartDriver_t的指针。此转换的结果是指向 eUartDriver_t 的指针,因此应用 *(如 * (eUartDriver_t *) args 中所示)引用 < code>eUartDriver_t,这是一个可用的类型。

Presumably args is declared to be a void *. The expression *args means “the thing args points to,” so *args would be a void, but void is not a usable type. So *args is bad code, and the compiler complains.

(eUartDriver_t *) args says “Convert the value of args to eUartDriver_t *”. That type is a pointer to an eUartDriver_t. The result of this conversion is a pointer to an eUartDriver_t, so applying *, as in * (eUartDriver_t *) args, refers to a eUartDriver_t, which is a usable type.

╭ゆ眷念 2025-01-17 12:49:11

@UnholySheep

操作顺序很重要。 *args 取消引用指针,因此之后可能无法将其转换为另一个指针类型(因为结果可能不是指针类型)

#include <stdio.h>

int main()
{
  //Consider integer:
  int x;
  //And pointer to it.
  int *p= &x;
  
  //These expressions
  p; &x;
  //are of type int*, which means the dereferencing operator can be used on these.
  //This is legal:
  *p; *(&x); p[0];
  
  //Pointer casting <==> reinterpretation of pointer (address)
  //These expressions are almost the same (have the same values and storage space)
  (int*)p; (char*)p; p; (void*)p;
  
  /*The only difference is in dereferencing or artmetics, which must happen 
  AFTER the pointer is known as (new) type pointer.
  
  So once you've got this expression:*/
  (char*)p;
  //or new variable:
  char *newP= (char*)p;
  //or macro:
  #define newP (char*)p
  
  //THAN you can do derefrencing with proper use of operators *ptr or ptr[offset]
  *newP; *((char*)p); newP[0]; ((char*)p)[0];
}

@UnholySheep

Order of operations matters a lot. *args dereferences the pointer, so casting that to another pointer type afterwards may not be possible (since the result is possibly not a pointer type)

#include <stdio.h>

int main()
{
  //Consider integer:
  int x;
  //And pointer to it.
  int *p= &x;
  
  //These expressions
  p; &x;
  //are of type int*, which means the dereferencing operator can be used on these.
  //This is legal:
  *p; *(&x); p[0];
  
  //Pointer casting <==> reinterpretation of pointer (address)
  //These expressions are almost the same (have the same values and storage space)
  (int*)p; (char*)p; p; (void*)p;
  
  /*The only difference is in dereferencing or artmetics, which must happen 
  AFTER the pointer is known as (new) type pointer.
  
  So once you've got this expression:*/
  (char*)p;
  //or new variable:
  char *newP= (char*)p;
  //or macro:
  #define newP (char*)p
  
  //THAN you can do derefrencing with proper use of operators *ptr or ptr[offset]
  *newP; *((char*)p); newP[0]; ((char*)p)[0];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文