• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Is it possible to subscript into a uint64_t pointer in C?

Resolved: Is it possible to subscript into a uint64_t pointer in C?

0
By Isaac Tonny on 18/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I’m a C beginner. Having trouble understanding whats happening with this code:
This prints 159.
I’m looking at uint8_t first_byte = ((uint8_t *)num_ptr)[0];
I’m trying to understand it this way: the uint64_t pointer num_ptr is first cast as a uint8_t pointer, then we index into it with the square brackets to get the first byte. Is this a correct explanation? If so is it possible to index into pointers to get their partial contents without dereferencing?

Answer:

  • 99999 = 0x1869F or if you will as a 64 bit number 0000 0000 0001 869F
  • Intel/PC computers use little endian. What is CPU endianness?
  • Therefore your 64 bit number is stored in memory as 9F86 0100 0000 0000.
  • C allows us to inspect a larger data type byte by byte through a pointer to a character type. uint8_t is a character type on all non-exotic systems.
  • ((uint8_t *)num_ptr)[0]; Converts the 64 bit pointer to a 8 bit (character) pointer and then uses the [] operator to de-reference that pointer.
  • We get the first byte 0x9F = 159 dec.
  • %hhu is used to print unsigned char. The most correct conversion specifier to use for uint8_t would otherwise be "%" PRIU8 from inttypes.h

If you have better answer, please add a comment about this, thank you!

Share. Facebook Twitter LinkedIn

Related Posts

Resolved: what are the permissions required to assign a resource monitor to a warehouse?

01/04/2023

Resolved: How can I modify formData before sending it?

01/04/2023

Resolved: How to efficient create SimpleITK image?

01/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.