IF I understand - one uses void * as the datatype in function arguments so that it can be cast to a fixed (read: one) datatype in the function.
If you choose to use anonymous blocks in the function, then you could use if() or switch() to select a datatype depending on another variable.
The best choice is to stick with a single datatype. If you need something "cool" use function pointers to branch to the right choice for the datatype you want:
You could go the way you're going with function pointers, but you may want to make the function prototype take two void * as arguments. While it isn't exactly type safe, it does allow you to call the function pointer without warnings regarding the types passed. If you don't do this, all your callers will need to know which of the two functions they are calling to pass the correct type safe parameters.
so something like:
I fixed the "list" argument, which needs to be a pointer to pointer since you are changing its value and the caller needs to see the new value for everything to work.
However, to be honest, I think all this is just a "waste" of time. You should concentrate on making a list package that is generic. Like, add_entity just takes a list and a void * for the data. It allocates nodes appropriately and you allocate the data you want to add to the list outside and pass it to an "add to list" function. The list entity just stores the next/prev pointers and a void * for the data item. In C++ you could use templates, in C you have to sacrifice type safety for the generic data item.
I have a function to which I will pass a struct ID and it will return me a string.
I will pass a pointer to store the name string and that pointer will be allocated memory by the function called.
int ConvertIDToName(void *id, void *name, size_t *size)
{
int status = 0;
... (5 Replies)
I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line
if
then... (8 Replies)
hi guys!
Is there such a thing as double void pointer dynamic allocation?
And if so is it something like this?
int n;
void** a;
a=malloc(n*sizeof(void*)); (12 Replies)
In a well-known book on the C language, there is an example of an efficient method for using a struct member as an argument to a function. (I'm a C noob, but I believe the correct terminology might be: use call-by-reference instead of call-by-value.) The function is printf.
Anyway, here's a... (5 Replies)
Hi All,
My latest assignment (practice not coursework!) is to write prototype interactive exam/test console application. I've used structs to store the question information (not sure if this was the best way to do it?) and I have the following code that outputs each question and it's possible... (0 Replies)
What is the difference between f(...) , f(void),f()
I know that f(void) doesn't take any parameters, but what about f() and f(...) Does the last call of function even exists? (2 Replies)
Hello all
im trying to build function that will return void function pointer
what is mean is ( not working )
the main function
void * myClass::getFunction(int type){
if(type==1)
return &myClass::Test1;
if(type==2)
return &myClass::Test2;
}
void myClass::Test1(){... (1 Reply)
First and foremost, this is not a homework for your information. I'm just new to using c-shell programming and I just wanted to make my life easier @ work.
Say, the file contains the following:
ID FILE NO. SL VP
1 1 22 33
1 2 ... (3 Replies)