*(< type> *)(< constant>)是什么意思? [英] What does *(<type>*)(<constant>) mean?

查看:164
本文介绍了*(< type> *)(< constant>)是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了以下C代码:

I recently came across the following C-code:

*(STRUCT_T*)(0xC6)

STRUCT_T typedef ed结构。有人可以解释这是什么意思?

STRUCT_T is a typedefed structure. Can someone explain what this means?

我的猜测: STRUCT_T * 投射地址 0xC6 到结构指针,然后 * 检索存储在地址 0xC6

My guess: STRUCT_T* casts the address 0xC6 to a structure pointer and * then retrieves the value (struct) stored at the address 0xC6?

推荐答案

是的,你是正确的,但我想,这个问题需要一个更详细的答案为什么

Yes, you're correct but I guess, this question needs a little more elaborated answer for why we are doing this.

首先,让我们来看一下 * 运算符。它取消引用它是基于操作数的类型的操作数。以非常简单的语言详细说明

To start with, let's see what is done by the unary * operator. It dereferences it's operand based on the type of the operand. To elaborate in very simple terms,


  • * ptr c> ptr 的类型 char * 将读取 sizeof(char) ie,1从 ptr

  • * ptr $ c> ptr 的类型 int * 将读取 sizeof(int) ptr
  • 开始的4字节数据(在32位系统上)
  • *ptr, when ptr is of type char * will read sizeof(char) i.e., 1 bytes of data starting from ptr
  • *ptr, when ptr is of type int * will read sizeof(int) i.e., 4 bytes of data (on 32 bit system) starting from ptr

,说 *(STRUCT_T *)(0xC6),我们正在执行


  • 将指针(地址) 0xC6 作为指向 STRUCT_T 的指针。

  • dereference相同,以获取 STRUCT_T 的值。

  • treat the pointer (address) 0xC6 as a pointer to type STRUCT_T.
  • dereference the same to get the value of type STRUCT_T.

这篇关于*(< type> *)(< constant>)是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆