Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use void pointer as function return type In C

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;
}
like image 287
Jack Zhu Avatar asked Nov 16 '13 08:11

Jack Zhu


People also ask

Can a function return a void pointer in C?

The malloc() and calloc() function return the void pointer, so these functions can be used to allocate the memory of any data type.

What does a void pointer return?

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.

What does void * function return?

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.

Is void a return type in C?

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.


1 Answers

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));
like image 168
tab Avatar answered Dec 08 '22 08:12

tab