There is a variable of type u_char *, the size is not clear. How to output it to the console in HEX format?

  • Display its contents - a pointer? - Harry
  • Not a pointer but content - Nikola Krivosheya
  • What does the pointer point to? But you yourself write "the size is not clear." So how many bytes to output? - Harry
  • output one character until meet \0 ? - Mirdin

1 answer 1

No You need to explicitly and separately obtain the size of the array and know the type of data stored in it.

The fact is that the pointer u_char * (as well as char * and void * ) usually means that starting from the specified place there is a buffer with raw data. What is in this buffer and what real type and size it is known only to the one who filled this buffer.

For example, the data on the pointer pointing to the uchar[]: 2a 4b 00 5a 8d ff 00 00 ... buffer uchar[]: 2a 4b 00 5a 8d ff 00 00 ... can be interpreted in a bunch of ways. For example:

  1. An array of N uchar indefinite length. And you cannot say where the elements end and another area of ​​memory begins.

  2. Array of N ints of indefinite length.

  3. Null-terminated ASCII string K* and garbage behind it.

  4. A null-terminated UTF16 string of some three characters.

  5. Some kind of structure. For example, struct {ina a; char b; short c;}; struct {ina a; char b; short c;}; (with or without field alignment).