uint32_t pointer to the same location as uint8_t pointer

#include <iostream>

int main(){
    uint8_t memory[1024];
    memory[0] = 1;
    memory[1] = 1;
    uint32_t *test = memory;
    //is it possible to get a value for *test that would be in this example 257?
}

I want to create a uin32_t pointer to the same adress as the uint8_t pointer. Is this possible without using new(adress)? I don't want to lose the information at the adress. I know pointers are just adresses and therefor I should be able to just set the uint32_t pointer to the same adress. This code produces an error:

invalid conversion from 'uint8_t*' to 'uint32_t*' in initialization

This would be a violation of so-called Strict Aliasing Rule, so it can not be done. Sad, but true.

Use memcpy to copy data and in many cases compilers will optimize memory copy and generate the same code as they would with cast, but in Standard-conforming way.

Convert uint8_t array to uint32_t array, sizeof(data)/4, encryption_key); radio.write(data, sizeof(data)); void btea(​uint32_t *v, int n) { } Any pointers (pun intended) are much appreciated. share. uint8_t is not a byte it's a octet, char is a byte, this is important because you are talking about C compliant code. In C, byte != octet, char could be greater than 8 bits, but uint8_t if defined has exactly 8 bits. Plus, uint8_t is not a character type so strict aliasing exception with character type theoretically don't work with uint8_t.

As already mentioned you cannot convert uint8_t * to uint32_t * due to strict aliasing rule, you can convert uint32_t * to unsigned char * though:

#include <iostream>

int main(){
    uint32_t test[1024/4] = {}; // initialize it!
    auto memory = reinterpret_cast<unsigned char *>( test );
    memory[0] = 1;
    memory[1] = 1;
    std::cout << test[0] << std::endl;
}

this is not portable code due to Endianness, but at least it does not have UB.

Casting types in C/C++ » JeeLabs, Now that we have pointers safely tucked into our tool belt, we can explore to a pointer to uint8\_t 's; (uint32_t) p : convert p (which could be a pointer!) to a But when cast to an int, it no longer acts in the same way – the value is the (int) 12.345 ” is the integer value 12; “ (float) 12345 ” is the floating point value 12345.0. #include <iostream> int main(){ uint8_t memory[1024]; memory[0] = 1; memory[1] = 1; uint32_t *test = memory; //is it possible to get a value for *test that would be in this example 257? I want to create a uin32_t pointer to the same adress as the uint8_t pointer.

This question completely ignores the concept of endian-ness; while your example has the lower and upper byte the same value, if the byte order is swapped it makes no difference; but in the case where it is; your number will be wrong unexpectedly.

As such, there's no portable way to use the resulting number.

Microcontrollers, Second Edition, (A pointer variable always occupies 2bytes of memory regardless ofthe data from uint8_t to uint32_t, thestarting memory location forau32_s is still 0x1000. Fixed width integer types (since C++11) From cppreference.com. < cpp ‎ | types. Standard Library Headers. Freestanding and hosted implementations. Named requirements. Language support library. Concepts library (C++20) Diagnostics library. Utilities library. Strings library. Containers library. Iterators library. Ranges library (C++20)

That can be done with a reinterpret_cast:

uint32_t *test = reinterpret_cast<uint32_t*>(memory);

Be aware however that you are using heavy ordnance here. Fiddling with memory representations like that is not recommended and can go wrong in surprising ways (say, alignment).

Type struct pointer issue - Keil forum - Software Tools, Why I cannot read uint32_t type from pointer? I know that maybe this kind of pointer cast is "brutal" way but I just want to read struct value in memory area not the same. struct S2E_Packet { uint8_t module_type[3]; uint8_t fw_ver[3]; uint32_t  Using an integer type prevents this dereferencing, thus avoiding bugs. Therefore, generic memory addresses in the kernel are usually unsigned long , exploiting the fact that pointers and long integers are always the same size, at least on all the platforms currently supported by Linux.

uint32_t *test =(uint32_t*) memory;

uint32_t shows that the memory pointed by test should contain uint32_t .

Ramblings about uint8_t and undefined behavior · GitHub, Thus it seems obvious to use unsigned char as the type of uint8_t , and that is If both the pointer operand and the result point to elements of the same array object #include <stdint.h> uint32_t read_le32(const uint32_t *val) { const uint8_t *p  Hi, Same question as the one I’ve posted before, Just concerning a different Type this time. I am wondering what would be the equivalent type to uint8_t in C#? I thought I saw similar types once within .NET (Either VB.NET or C#) but can’t find it, and Visual Studio Suggestions are clueless.

4. Type Conversions - C in a Nutshell [Book], For example, a pointer to a structure can be converted into a different pointer type​. Every complex floating-point type has the same rank as the type of its real  A direct variable is bound to a block of addresses in memory that holds a piece of data. [code]int x = 5; /* x is bound to a 4-byte region of memory, set aside to hold an int. */ /* the binary value 00000000 0

https://social.msdn.microsoft.com/Forums/en-US/2fb, Do not convert a pointer value to a pointer type that is more strictly aligned than the network stacks) must be copied to a properly aligned memory location, such which is new to C11, to declare the char object c with the same alignment as  Now that we have pointers safely tucked into our tool belt, we can explore some of the more esoteric sides of pointers. And yes, dear reader, there are definitely a couple… In C and C++, pointers are typed. The address could be anything, but the way pointers are declared, you have to include a specification of the type of what it points to.

EXP36-C. Do not cast pointers into more strictly , int8_t, uint8_t, Integer type with a width of exactly 8, 16, 32, or 64 bits. int32_t, uint32_t intptr_t, uintptr_t, Integer type capable of holding a value converted from a void pointer and then Some of these typedefs may denote the same types.

Comments
  • do you mean you want something like uint_t test=memory[0]; but for an array?
  • Is that really the exact error message?
  • I would be afraid of breaking the strict aliasing rule, but I should mention that I never fully understood that rule
  • @JoshDetwiler That won't work.
  • @user463035818 uint8_t is most probably an alias to unsigned char so aliasing rule should be fine
  • Some compilers offer options to disable the strict aliasing rule, like -fno_strict_aliasing. (But in most cases it does not make sense to use them IMHO).
  • @alain disabling this is often a terrible pessimization
  • Yes, I agree. The only use case I see is if you have a very large legacy code base that you're unwilling to change at the moment.
  • You might as well explain why it is not portable (not evident for everyone, I suppose)
  • It could be noted that uintxx_t types when they exist are required to have exactly xx bits and no padding bits. So if that code is compiled, the result is implementation defined because of endianness, but no UB is involved and no trap representation is possible. Simply, an implementation is free to not define the uintxx_t types...
  • Aside endianness uint32_t is 4 bytes, OP assigns only 2. I would see that as bigger problem here.