Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要帮助理解指针变量赋值吗 我不擅长高级C++脚本。我试图了解更多关于以下变量分配的信息,但没有成功。请解释它们或给我一个来源来研究类似的陈述 rand_seed = *(int*)input_buffer_ptr; moving_input_ptr = (BYTE*)((int*)input_buffer_ptr + 1);_C++_Pointers - Fatal编程技术网

需要帮助理解指针变量赋值吗 我不擅长高级C++脚本。我试图了解更多关于以下变量分配的信息,但没有成功。请解释它们或给我一个来源来研究类似的陈述 rand_seed = *(int*)input_buffer_ptr; moving_input_ptr = (BYTE*)((int*)input_buffer_ptr + 1);

需要帮助理解指针变量赋值吗 我不擅长高级C++脚本。我试图了解更多关于以下变量分配的信息,但没有成功。请解释它们或给我一个来源来研究类似的陈述 rand_seed = *(int*)input_buffer_ptr; moving_input_ptr = (BYTE*)((int*)input_buffer_ptr + 1);,c++,pointers,C++,Pointers,(考虑到int为4字节) 将RAM想象成一条长字节行(因为它是): 和SOME_TYPE*作为某个字节上的指针: .... [8bit][8bit][8bit][8bit][8bit][8bit][8bit][8bit][8bit][8bit] .... ^ input_buffer_ptr int*表示将此指针下的数据视为整数(大小为4字节的数量) 因此,如果您有指针某些类型的*input\u buffer\u ptr (int*)input_buffer_

(考虑到
int
为4字节)

将RAM想象成一条长字节行(因为它是):

SOME_TYPE*
作为某个字节上的指针:

.... [8bit][8bit][8bit][8bit][8bit][8bit][8bit][8bit][8bit][8bit] ....
           ^
      input_buffer_ptr
int*
表示将此指针下的数据视为整数(大小为4字节的数量)

因此,如果您有指针
某些类型的*input\u buffer\u ptr

(int*)input_buffer_ptr; // casts this pointer to int*, 
     //so now you treat data under this pointer as 4 bytes integer
然后:

因此,
rand\u seed
是整数,具有以下值:

.... [8bit][8bit][8bit][8bit][8bit][8bit][8bit][8bit][8bit][8bit] ....
           |      random_seed     |
然后:

因此:

然后:

 (BYTE*)((int*)input_buffer_ptr + 1);
// ^
// casting pointer to BYTE*, so it points to the same place
// but now treated as one byte pointer.
因此,如果您尝试以下方法:

BYTE a = *(BYTE*)((int*)input_buffer_ptr + 1);
您将获得一个带值的字节变量:

.... [8bit][8bit][8bit][8bit][8bit][8bit][8bit][8bit][8bit][8bit] ....
                                   |  a  |

第一个表达式非常简单:在
int*
cast之后提取
input\u buffer\u ptr
指针的第一项。
rand_seed
应该是一个“int”。非常感谢@Yevhen Kuzmovych的详细回答。注意:这是未定义的行为(严格的别名冲突),除非缓冲区最初也是通过
int
编写的。
.... [8bit][8bit][8bit][8bit][8bit][8bit][8bit][8bit][8bit][8bit] ....
                                   ^
                         ((int*)input_buffer_ptr + 1)
 (BYTE*)((int*)input_buffer_ptr + 1);
// ^
// casting pointer to BYTE*, so it points to the same place
// but now treated as one byte pointer.
BYTE a = *(BYTE*)((int*)input_buffer_ptr + 1);
.... [8bit][8bit][8bit][8bit][8bit][8bit][8bit][8bit][8bit][8bit] ....
                                   |  a  |