C语言 什么是 *(uint32_t*)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48833976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is *(uint32_t*)
提问by Hoa.N
I have hard time to understand *(uint32_t*).
我很难理解*(uint32_t*)。
Let say that I have
假设我有
uint32_t* ptr;
uint32_t num
*(uint32_t*)(ptr + num); //what does this do? does it
回答by Pablo
uint32_tis 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是保证 32 位的数字类型,该值是无符号的,这意味着值的范围从 0 到 2 32- 1。
This
这个
uint32_t* ptr;
declares a pointer of type uint32_t, but the pointer is uninitialized, that
mean 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,但该指针未初始化,这意味着该指针并未特别指向任何地方。尝试通过该指针访问内存将导致未定义的行为,并且您的程序可能会崩溃。
This
这个
uint32_t num;
is just a variable of type uint32_t.
只是一个类型的变量uint32_t。
This
这个
*(uint32_t*)(ptr + num);
ptr + numreturns 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 + numas the memory address based on the original
ptrpointer plus the number of bytes for numuint32_tobjects.
ptr + num返回一个新的指针。它被称为指针算术,就像常规算术一样,只是编译器考虑了类型的大小。可以认为是ptr + num基于原始ptr指针的内存地址
加上numuint32_t对象的字节数。
The (uint32_t*) xis a cast, this tells the compiler that it should treat the
expression xas if it were a unit32_t. In this case it's not even needed
because ptr + numis already a uint32_t*.
The (uint32_t*) xis a cast,这告诉编译器它应该将表达式x视为unit32_t. 在这种情况下,它甚至不需要,因为ptr + num它已经是一个uint32_t*.
The *at the beginning is the dereferencing operator which is used to access
the memory throug a pointer. The whole expression is equivalent to
的*开头是解除引用运算符,它用于访问存储器通过量的指针。整个表达式等价于
ptr[num];
Now because none of this variables are initialized, the results 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));
this would print 5, because ptr[2]is 5.
这将打印 5,因为ptr[2]是 5。
回答by user7610
uint32_tis defined in stdint.h, so one may need to include it
uint32_t在 中定义stdint.h,因此可能需要包含它
#include <stdint.h>
回答by giusti
This doesn't really do anything. Let me give you a different example:
这真的没有任何作用。让我给你一个不同的例子:
uint32_t data;
void *pointer = &data;
*(uint32_t *)pointer = 5;
First of all, void*means "generic" pointer. It can point to objects of any type.
首先,void*表示“通用”指针。它可以指向任何类型的对象。
Now, (uint32_t *)means "interpret pointeras a pointer to an object with type uint32_t.
现在,(uint32_t *)表示“解释pointer为指向类型为 的对象的指针uint32_t。
The rest of the expression simply means "store 5 at the location stored by this pointer".
表达式的其余部分仅表示“在此指针存储的位置存储 5”。
If you want to know what uint32_tis, that's an unsigned integer with exactly 32 bits. And pointer + numis the same as the adress of pointer[5].
如果你想知道是什么uint32_t,那是一个 32 位的无符号整数。并且pointer + num与 的地址相同pointer[5]。

