How to pass this parameter to a void function?

Asked

Viewed 136 times

0

Having two roles being the first :

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

Already the binary convert is a little big so I’ll explain, basically it converts an integer number to binary. I’m not able to compile the code, because I don’t know how to pass K as a parameter to the converte_binario function. I’ve tried to:

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

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

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

void converte_binario (int n);
  • You should use this: k = * ((int * ) end_byte);

1 answer

0


Its function print_bytes receives a pointer in end_byte. So you can assign to k the entire value to which that pointer points you must make:

k = * (int*)end_byte;

This syntax is telling the compiler that you are trying to de-reference a pointer that points to an integer (the type of k). Like end_byte is a pointer to void, you must first convert to an integer pointer, so that the compiler knows how many bytes to use at deregister time: (int*)end_byte.

Then you can call the function converte_binario with:

converte_binario(k);

Thus, the binary value cannot be stored in k. This means: you will not have access to the converted value from within the function print_bytes.

If the function print_bytes do not need to know the converted binary, this is no problem. Otherwise, you should implement a way for this value to be returned (either by passing a pointer as argument and writing the binary on that pointer or returning something from the function converte_binario).

  • Even putting k = (int) *end_byte the compiler accuses the following errors : "Invalid use of void Expression" and "Dereferencing 'void * 'Pointer"

  • My fault: the right one would be k = * (int*)end_byte;. I will correct the answer and explain why.

  • Now you’ve solved the problem, vlw

Browser other questions tagged

You are not signed in. Login or sign up in order to post.