I have a hard time understanding *(uint32_t*)
.
Let's say that I have:
uint32_t* ptr; uint32_t num *(uint32_t*)(ptr + num); // What does this do? Does it
The UInt32 value type represents unsigned integers with values ranging from 0 to 4,294,967,295. UInt32 provides methods to compare instances of this type, convert the value of an instance to its String representation, and convert the String representation of a number to an instance of this type.
The suffix _t denotes a type name. For example, size_t is the type for size. Another popular line of thought regards it as a convention for naming type definitions. Follow this answer to receive notifications.
UInt32 is used to represent 32-bit unsigned integers. UInt64 is used to represent 64-bit unsigned integers.
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.
This
uint32_t* ptr;
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.
This
uint32_t num;
is just a variable of type uint32_t
.
This
*(uint32_t*)(ptr + num);
ptr + num
returns you a new pointer. It is called pointer arithmetic. It's like regular arithmetic, only that compiler takes the size of 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.
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));
this would print 5, because ptr[2]
is 5.
uint32_t
is defined in stdint.h
, so one may need to include it
#include <stdint.h>
this header shall define uint32_t
to be an unsigned integer type taking exactly 32 bits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With