warning: dereferencing `void *' pointer

From: Pushkar Pradhan (pushkar_at_gri.msstate.edu)
Date: 12/14/03


Date: Sat, 13 Dec 2003 21:24:16 -0600

I have some functions which take as i/p a buffer (it can be float, char,
or 16 bit, int etc.). The result is another o/p buffer, its type is also
flexible (it could be a float, char etc.).

I try to pass both as "void *buf" so that it can accept any data type.
But since I access the buffer and try to assign its elements to another
I get compile errors (I have pasted at the end).

Now my question is how can I pass the i/p and o/p buffers as args to the
function without hardcoding the data type, someone earlier suggested to
pass another argument which tells the type, e.g.
myfunction(void * buf, 1, ...) 1 may indicate that buf is char type, 2
means 16 bit etc.

But that means having if .. else's inside the function to do the same
thing. Is there any other way?
Maybe this is not possible in C.
Pushkar Pradhan

resample.c: In function `resample':
resample.c:26: warning: dereferencing `void *' pointer
resample.c:26: warning: dereferencing `void *' pointer
resample.c:26: invalid use of void expression
resample.c:35: warning: dereferencing `void *' pointer
resample.c:35: warning: dereferencing `void *' pointer
resample.c:35: invalid use of void expression



Relevant Pages

  • Re: The void** pointer breaking symmetry?
    ... void** is a generic pointer type that can be implicitly converted ... because dereferencing the void ** variable once gives ...
    (comp.lang.c)
  • Re: Type-checking casts for GNU C
    ... I don't think *x is valid when x is a void*, ... operand must have pointer type. ... But why would it allow dereferencing of a void* (even when its "treat ... the other cast macro, T must be a pointer type in this one, so you just ...
    (comp.lang.c)
  • Re: (maybe DR) lvalueness of dereferencing void*
    ... >> the definition of lvalue (6.3.2.1p1; lvalues don't have void type). ... > A void pointer does not point to an object and dereferencing it does ...
    (comp.std.c)
  • Re: (maybe DR) lvalueness of dereferencing void*
    ... the result is an lvalue designating the object. ... > the definition of lvalue (6.3.2.1p1; lvalues don't have void type). ... If the operand has type "pointer to type", ... A void pointer does not point to an object and dereferencing it does ...
    (comp.std.c)
  • Re: (maybe DR) lvalueness of dereferencing void*
    ... >> A void pointer does not point to an object and dereferencing it does ... > If it does not point to an object, then where does the Standard ...
    (comp.std.c)