Quote:
>Am writing a generic function which will return pointers to void of
>the form.
>test(void **_void_pntr)
It is a good idea always to include return types for function. Since this
doesn't appear to return a value make that:
void test(void **_void_pntr)
Quote:
>{
> if (blah blah)
> *_void_ptr=&_float;
> else
> *_void_ptr=&_char;
>}
What are _float and _char here? Identifiers that begin with an underscore are
reserved at file scope. Since you haven't defined them within a function
there is no way these can be valid. It is generally best to avoid identifiers
with leading underscores.
Assuming that these represent objects of type float and char note that the
assignments here cause pointers to these to be converted to void * and
it is those void * pointers that are being written.
Quote:
>main()
Better written as:
int main(void)
Quote:
>{
> float * _float1
> char * _char1;
> test(&_float1);
Here you are trying to pass a pointer to a pointer to float to test where
it requires a pointer to pointer to void. This is invalid in C. To correct
this define _float1 with type void *.
Quote:
> printf("%f", *(float*)_float1);
With that change the cast here now makes sense (it was redundant before).
Quote:
> test(&_char1);
> printf("%c", *(char*)_char);
Similarly here. It might be better to write:
void *test(void)
{
if (blah blah)
return &_float;
else
return &_char;
Quote:
}
which allows you to assign the returned void * value directly to the
appropriate type of pointer in the caller.
Quote:
>}
>My worry is the lack of type checking.
What type checking do you want?
Quote:
>Ibelieve it is possible to do
>some run-time type checking via the use of macros.
C doesn't use any runtime type information, it has a static type system
which is resolved at compile time. Macros are a source code construct.
Quote:
>My dodgey memory seems to recall a 'typeof' call but my VC++ (No
>access to a unix box at the moment curse it all) doesn't seem to know
>about it and seems to infer thst it's a VC++ extension.
Probably, there is no typeof operator in the C language. Some compilers
do define it as an extension but from what I've seen it is still a source
level operation.
Quote:
>Please reply if you have any suggestions ( deadline looming). I'm also
>cut off from email so you'll have to post.
What is it that you are trying to guard against? Certainly void * can
create a bit of a hole in type checking but generally you just have to be
aware of it and be careful. In your example you have a test on (blah blah)
which determines which pointer is used. There is no portable way to
validate that against the type of arguments passed to the function (since
by the time the function sees them they are all void ** anyway).
--
-----------------------------------------
-----------------------------------------