Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
whenever we pass array name as the parameter to the function, it works as a pointer to the first element of the array
We can make this array name as void pointer by (void*)array

Suppose if we pass like this,
func(parameter1,(void*)array)

What is the use of this?
In which case we use like this? and what is the benefit?

What I have tried:

Searched google but unable to get it........
Posted
Updated 27-Jun-21 7:37am

Pointers are always the same size for a given application (i.e. they are either all 32 bit or all 64 bit: you don't get mixed sizes. So in essence, a pointer is a pointer, it doesn't actually matter what it points to (well ... it does because the alignment of the type it points to can cause problems if you aren't careful).

A void pointer means it can accept any pointer type:
void foo(void *p)
    {
    printf("%p\n", p);  
    } 
int main()
    {
    int x[10];
    char c[5] = "abcde";
    void* p = x;
    p = c;
    foo(x);
    foo(c);
    return 0;
    }


So if you are planning on creating a function that can take different types of data, it can be handy to pass it to a void pointer parameter.
They are also useful when you want to return "anonymous" data because the caller must cast it to something useful to use it - just as you have seen with malloc
 
Share this answer
 
Comments
_-_-_-me 27-Jun-21 9:19am    
oh , okay .
Thank you very much . You explained it very clearly . I am grateful to you for the help!
OriginalGriff 27-Jun-21 9:50am    
You're welcome!
It can be used to allow a function to accept any pointer type. For example:
C++
int myfunc(void* foo)
{
// do some things with the data

    return answer;
}

int main()
{
    int intarray[] = { 1, 2, 3 };
    char chararray[] = { '1', '2', '3' };
   
    myfunc(intarray);
    myfunc(chararray);

    return 0;
}

If myfunc declared its parameter as int* then the compiler would reject the second call. But remember that myfunc must have some way of determining what type of data is in the array, there is no mechanism in the language that will help it.
 
Share this answer
 
Comments
_-_-_-me 27-Jun-21 9:23am    
oh, okay .
Thank you very much !
You explained it very clear.
I am so grateful for all the help!
Richard MacCutchan 27-Jun-21 9:40am    
you're welcome. I have no idea where I first learnt this and cannot find any mention of it in Kernighan & Ritchie.
it is always a bad idea to use void pointers because the compiler wont help you anymore. On typed pointers there is check on type casts.

Be aware: when you write correct code nothing will happen, but if you mix type you got "a helping friend" which tells you: Dont do that. Maybe you need that experience of strang bugs ;-)
 
Share this answer
 
Comments
k5054 27-Jun-21 13:46pm    
Quote:it is always a bad idea to use void pointers because the compiler wont help you anymore. True enough, but sometimes with C, there's no other option. C does not have any concept of generics, so resorting to a void* is the best a C programmer can do. Even the standard library needs to rely on this for functions like qsort() and bsearc().

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900