*(< type> *)(< constant>)是什么意思? [英] What does *(<type>*)(<constant>) mean?
问题描述
我最近遇到了以下C代码:
I recently came across the following C-code:
*(STRUCT_T*)(0xC6)
STRUCT_T
是 typedef
ed结构。有人可以解释这是什么意思?
STRUCT_T
is a typedef
ed 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
, whenptr
is of typechar *
will readsizeof(char)
i.e., 1 bytes of data starting fromptr
*ptr
, whenptr
is of typeint *
will readsizeof(int)
i.e., 4 bytes of data (on 32 bit system) starting fromptr
,说 *(STRUCT_T *)(0xC6)
,我们正在执行
- 将指针(地址)
0xC6
作为指向STRUCT_T
的指针。 - dereference相同,以获取
STRUCT_T
的值。
- treat the pointer (address)
0xC6
as a pointer to typeSTRUCT_T
. - dereference the same to get the value of type
STRUCT_T
.
这篇关于*(< type> *)(< constant>)是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!