Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between (void*) and (void(*)(argument type)) cast? [duplicate]

Tags:

c++

c

pointers

void funcPtr(int a);

int main(){
   int k=1;
   void (*funcPtr2)(int);

   funcPtr2 = (void*)(funcPtr);
   // funcPtr2 = (void(*)(int))(funcPtr);

   (*funcPtr2)(k);
   return 0;
}

void funcPtr(int a){
   printf("%d", a);
}

What is the difference between (void*) and (void(*)(argument type) in function pointer type casting?

As a result, it does not occur warning.

Is this wrong? about (void*) type casting

like image 909
양진혁 Avatar asked Jun 01 '15 10:06

양진혁


People also ask

What does void * func () mean?

Void functions are created and used just like value-returning functions except they do not return a value after the function executes. In lieu of a data type, void functions use the keyword "void." A void function performs a task, and then control returns back to the caller--but, it does not return a value.

What is the significance of void * in C?

It points to some data location in the storage. This means it points to the address of variables. It is also called the general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.

Is void the same as ()?

It seems like () and Void both mean exactly the same thing (the type Void) when used in the context of a type declaration, but in all other contexts, () instead means an instance of the type Void, ie the same as Void().

What does casting to void do C++?

Casting an expression to void explicitly throws away that expression's value. This tells readers that the programmer intended to write a do-nothing expression.


2 Answers

Is this wrong? about (void*) type casting

Yes, it is.

C standard doesn't allow a conversion of a function pointer to an object pointer or assignment between them. If you up your compiler warning level, you may get warnings/errors such as compiling with:

gcc -Wall -Wextra -pedantic-errors -std=c11 file.c

I am not sure why you thought about casting a function pointer. Provided the function pointer type matches the function, simply assign it:

 funcPtr2 = funcPtr;

Aside:

You can use the function pointer just like a function:

 funcPtr2(k);

and use a standard prototype for main such as:

int main(void)
like image 165
P.P Avatar answered Nov 14 '22 20:11

P.P


This is an issue with dlsym, declared void *dlsym(void *restrict handle, const char *restrict name). dlsym returns a pointer to a function named name defined in and accessible through handle.
However, as @BlueMoon states, this is not defined in ISO C and gcc indeed complains if made pedantic. POSIX tried to address that and required conforming implementation to make that cast well-defined in its indefiniteness.

From POSIX

Note that conversion from a void * pointer to a function pointer as in:

fptr = (int (*)(int))dlsym(handle, "my_function");

is not defined by the ISO C standard. This standard requires this conversion to work correctly on conforming implementations.

Another way to make that cast conforming is to first convert the function pointer to void** and then dereference it. This should be well-defined. In your case,

void funcPtr(int a); /* symbol to extract */

int main(void)
{
     void (*fn)(int);
     *(void**)(&fn) = dlsym(your_handle, "funcPtr");


     fn(5);
}
like image 23
edmz Avatar answered Nov 14 '22 22:11

edmz