How to pass this parameter to a void function?

0

Having two functions being the first:

void print_bytes (const void * end_byte, int n){
    int k;
    k = end_byte;
    converte_binario(k);
}

Converting binary is a bit big so I'll explain, basically it converts an integer to binary. I am not able to compile the code, as I do not know how to pass K as a parameter to the function convert_binary. Already tried:

k = *end_byte;
k = (int)end_byte;
k = (int*)end_byte;

And all give error, I would like to know how to pass K or even end_byte as a parameter to the function convert_binary.

PS: Here is the "prototype" of the function convert_binary.

void converte_binario (int n);
    
asked by anonymous 06.06.2017 / 01:21

1 answer

0

Its function print_bytes gets a pointer in end_byte . For you to be able to assign to k the integer value that that pointer points you should do:

k = * (int*)end_byte;

This syntax tells the compiler that you are trying to dereference a pointer that points to an integer (the type of k ). Since end_byte is a pointer to void , you must first convert to a pointer to integer, so that the compiler knows how many bytes to use at the time of dereferencing it: (int*)end_byte .

Then you can call the function converte_binario with:

converte_binario(k);

In this way, the binary value can not be stored in k . This means you will not have access to the converted value, from within the print_bytes function.

If the print_bytes function does not need to know the converted torque, this is not a problem. Otherwise, you must implement a way that this value is returned (either passing a pointer as an argument and writing the binary on that pointer or returning something from the converte_binario function).

    
06.06.2017 / 01:29