什么是*(uint32_t *) [英] What is *(uint32_t*)
问题描述
我很难理解*(uint32_t*)
.
让我说:
uint32_t* ptr;
uint32_t num
*(uint32_t*)(ptr + num); //what does this do? does it
推荐答案
uint32_t
是一种数字类型,可以保证32位,该值是无符号的,
表示值的范围从0到2 32 -1.
uint32_t
is a numeric type that guarantees 32 bits, the value is unsigned,
meaning that the range of values goes from 0 to 232 - 1.
此
uint32_t* ptr;
声明类型为uint32_t*
的指针,但该指针未初始化,即
是,指针没有特别指向任何地方.尝试访问
通过该指针的内存将导致未定义的行为,并且您的程序
可能会崩溃.
declares a pointer of type uint32_t*
, but the pointer is uninitialized, that
is, the pointer does not point to anywhere in particular. Trying to access
memory through that pointer will cause undefined behaviour and your program
might crash.
此
uint32_t num;
只是类型uint32_t
的变量.
此
*(uint32_t*)(ptr + num);
ptr + num
返回一个新的指针.这就是所谓的指针算术
像常规算术一样,只是编译器将类型的大小放入
考虑.将ptr + num
视为基于原始地址的内存地址
ptr
指针加上num
uint32_t
对象的字节数.
ptr + num
returns you a new pointer. It is called pointer arithmetic, it's
like regular arithmetic only that compiler takes the size of a types into
consideration. Think of ptr + num
as the memory address based on the original
ptr
pointer plus the number of bytes for num
uint32_t
objects.
(uint32_t*) x
是强制类型转换,它告诉编译器应将
表达式x
,就好像它是uint32_t*
.在这种情况下,甚至不需要
因为ptr + num
已经是uint32_t*
.
The (uint32_t*) x
is a cast, this tells the compiler that it should treat the
expression x
as if it were a uint32_t*
. In this case it's not even needed
because ptr + num
is already a uint32_t*
.
开头的*
是用于访问的解引用运算符
通过指针存储.整个表达式等于
The *
at the beginning is the dereferencing operator which is used to access
the memory through a pointer. The whole expression is equivalent to
ptr[num];
现在,由于这些变量均未初始化,因此结果将是垃圾. 但是,如果您这样初始化它们:
Now, because none of these variables is initialized, the result will be garbage. However if you initialize them like this:
uint32_t arr[] = { 1, 3, 5, 7, 9 };
uint32_t *ptr = arr;
uint32_t num = 2;
printf("%u\n", *(ptr + num));
这将打印5,因为ptr[2]
是5.
this would print 5, because ptr[2]
is 5.
这篇关于什么是*(uint32_t *)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!