1

ill start by saying ive seen a bunch of posts with similar titles but non focus on my question

ive been tasked to make a function that receives a void* arr, unsigned int sizeofArray and unsigned int sizeofElement i managed to iterate through the array with no problem, however when i try to print out the values or do anything with them i seem to get garbage unless i specify the type of them beforehand

this is my function:

 void MemoryContent(void* arr, unsigned int sizeRe, unsigned int sizeUnit)
{
int sizeArr = sizeRe/sizeUnit;

for (int i = 0; i < sizeArr ; i++)
{
    
    printf("%d\n",arr); // this one prints garbage
    printf("%d\n",*(int*)arr); // this one prints expected values given the array is of int*
    arr = arr + sizeUnit;
}

}

the output of this with the following array(int arr[] = {1, 2, 4, 8, 16, 32, -1};) is:

-13296 1
-13292 2
-13288 4
-13284 8
-13280 16
-13276 32
-13272 -1

i realize i have to specify somehow the type. while the printf wont actually be used as i need the binary representation of whatever value is in there (already taken care of in a different function) im still not sure how to get the actual value without casting while knowing the size of the element any explanation would be highly appreciated!

note: the compiler used is gcc so pointer arithmetics are allowed as used

edit for clarification: the output after formating and all that should look like this for the given array of previous example

00000000 00000000 00000000 00000001 0x00000001
00000000 00000000 00000000 00000010 0x00000002
00000000 00000000 00000000 00000100 0x00000004
00000000 00000000 00000000 00001000 0x00000008
00000000 00000000 00000000 00010000 0x00000010
00000000 00000000 00000000 00100000 0x00000020
11111111 11111111 11111111 11111111 0xFFFFFFFF
Flyleaf
  • 39
  • 5
  • 1
    Re “how to get the actual value without casting while knowing the size”: How would that be possible? Knowing something’s size, how many bytes it has, does not tell you how those bytes encode the value. The bits in the object could be unsigned binary for the value. Or they could be two’s complement for the value. Or they could be a floating-point encoding, which has multiple subfields. Or they could be individual bytes that encode characters for a printable string. Or they could be a multi-byte character encoding for, say, a French character set. The type tells us the encoding, so we need it. – Eric Postpischil Jan 06 '22 at 18:18
  • Do you just want to show the raw bytes of something, regardless of what value they encode? Then use `unsigned char` and print the individual bytes, typically in hexadecimal (use `%hhx`). – Eric Postpischil Jan 06 '22 at 18:20
  • 1
    `printf("%d\n",arr); // this one prints garbage` because you are passsing a pointer where an `int` is expected. – Weather Vane Jan 06 '22 at 18:20
  • @EricPostpischil im not sure honestly, but i need to get its binary and hexa values... and it should work for all types your second comment takes care of the hexa part and i might be able to convert from the hexa to binary ill try and report back, thank you – Flyleaf Jan 06 '22 at 18:32

1 Answers1

3

getting values of void pointer getting values of void pointer while only knowing the size of each element

Not possible getting values of void pointer while only knowing the size of each element.

Say the size is 4. Is the element an int32_t, uint32_t, float, bool, some struct, or enum, a pointer, etc? Are any of the bits padding? The proper interpretation of the bits requires more than only knowing the size.

Code could print out the bits at void *ptr and leave the interpretation to the user.

unsigned char bytes[sizeUnit];
memcpy(bytes, ptr, sizeUnit);
for (size_t i = 0; i<sizeof bytes; i++) {
  printf(" %02X", bytes[i]);
}

Simplifications exist.


OP's code void* arr, ... arr = arr + sizeUnit; is not portable code as adding to a void * is not defined by the C standard. Some compilers do allow it though, akin to as if the pointer was a char pointer.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • hmm i see what you mean. maybe i misunderstood the assignment or im looking at it the wrong way what if i just wanted to get the binary value of each bit? my idea was to convert them manually but that requires me to know the type can bit manipulation get me the bit data that i need? – Flyleaf Jan 06 '22 at 18:47
  • 1
    @Flyleaf This answer's code provides the printed data in hexadecimal. Try that first. Once you have that working, then let us look to getting binary (ones and zeros) output. – chux - Reinstate Monica Jan 06 '22 at 18:50
  • 1
    @Flyleaf [this](https://stackoverflow.com/a/34641674/2410359) may help you print anything in binary. – chux - Reinstate Monica Jan 06 '22 at 20:09