I'm trying to create a type generic function in C. As an argument I was passing on a void pointer. (The programmer using the function would be able to pass on a pointer of any type). However, this of course presents problems when dereferencing said pointer in the function. Any ideas as to how I can solve this problem? Thanks!
            Asked
            
        
        
            Active
            
        
            Viewed 133 times
        
    0
            
            
        - 
                    2Paste the sample code. – sanjeev mk Dec 22 '13 at 08:55
- 
                    cast it and it should work ;) – Daij-Djan Dec 22 '13 at 08:55
- 
                    Add a `(char*)p` cast if you have declared a `void* p` parameter. – Basile Starynkevitch Dec 22 '13 at 08:55
- 
                    This --> [SO post][1] explains everything nicely [1]: http://stackoverflow.com/questions/692564/concept-of-void-pointer-in-c-programming – sanjeev mk Dec 22 '13 at 08:58
- 
                    Possibly related: http://stackoverflow.com/questions/9804371/syntax-and-sample-usage-of-generics-in-c11 – XORcist Dec 22 '13 at 09:14
- 
                    @BasileStarynkevitch: would that make the function generic though? (ex: would it work with floats and doubles?) – user3126802 Dec 22 '13 at 09:29
- 
                    @user3126802 a void pointer does not know what it points to. you have to encode the type in the information, e.g. by using a struct, or use non-generic functions. those are probably easier to maintain anyway. – Andreas Grapentin Dec 22 '13 at 09:52
- 
                    @AndreasGrapentin I am actually using a struct which contains a void pointer. But I still cannot dereference the pointer no? – user3126802 Dec 22 '13 at 10:00
- 
                    @user3126802 no. but if the struct contains information about the type of the information, then you can cast the void pointer to a pointer of the right type – Andreas Grapentin Dec 22 '13 at 11:26
