So I am planing to Write a function to return a random array element. The function accept two parameters—an array of void pointers and the array length. It should return a void pointer. The idea is to take the given array, which comes in the form of an array of void pointer, the function will return an random element of the the array. The question I have is, what do I need to do to return a pointer, what do I need to do to the "result" so I can return it like that? After wards what do I need to do to access it again? Thanks
Here is what I have done, but I'm getting the:
"25: error: invalid use of void expression"
with warnings like:
" warning: pointer of type ‘void *’ used in arithmetic"
My code:
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
void *randomNum(void * array, int length)
{
void * result;
result=array[rand()%(length-1)];
return result;
}
int main()
{
int i;
srand(13);
int array[9]={1,5,6,85,132,65463,1354,5863,134};
for (i=0;i<9; i++)
{
printf("%d\n",*randomNum(array,9));
}
return 0;
}
The malloc() and calloc() function return the void pointer, so these functions can be used to allocate the memory of any data type.
It points to some data location in the storage. This means that it points to the address of variables. It is also called the general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.
A void function cannot return any values. But we can use the return statement. It indicates that the function is terminated. It increases the readability of code.
In computer programming, when void is used as a function return type, it indicates that the function does not return a value. When void appears in a pointer declaration, it specifies that the pointer is universal. When used in a function's parameter list, void indicates that the function takes no parameters.
You can write a perfectly fine generic function this way, but:
result = array[rand()%(length-1)];
This is dereferencing the void pointer array
while also attempting to store it into a pointer result
. What you want to store is the address at that offset:
result = array + rand()%(length-1);
However, you can't perform arithmetic like this on void pointers either, as the size of the underlying type is not known (some compilers allow sizeof(void)==1
as an extension). With a non-void array, the number of bytes a given element consumes, and thus the number of bytes to increment by when doing arithmetic on the address, is encoded in the type. In a generic function operating on void pointers like this one you'll need to explicitly pass the size of the type.
void *randomNum(void * array, size_t size, size_t length)
Now perform the calculation by casting array
to a char pointer, which forces arithmetic on array
to occur in increments of 1 byte times the provided size parameter:
result = (char*)array + (rand()%(length-1)) * size;
^ ^
You can then call randomNum with randomNum(array, sizeof(*array), 9)
However, you still need to cast the return value of the function before dereferencing it.
printf("%d\n", *(int*)randomNum(array,sizeof(*array),9));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With