我最近遇到了以下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>)是什么意思?
发布评论