doubleE
doubleE

Reputation: 1063

C Function Pointer assignment

As far as I read when you declare a function pointer, there is not assignment to a left hand side or right hand side. But I have numerous function pointers in a C source file and all are used in an assignment form like below:

void (*pbindRemoveDev)( zAddrType_t *Addr ) = (void*)NULL;

Can anybody can help me understand (void*)NULL part?

This is an embedded code.

Upvotes: 1

Views: 436

Answers (3)

Victor Lopez
Victor Lopez

Reputation: 110

It is a function pointer declaration. Your code initializes it to NULL.

void (*pbindRemoveDev)( zAddrType_t *Addr ) = (void*)NULL;

Unaware the compiler you're using, proper initializationn could be

void (*pbindRemoveDev)( zAddrType_t *Addr ) = NULL;

If you invoke the function pointer it will crash since you're pointing to NULL memory address.

Below is a function pointer initialization example:

 #include <stdio.h>

 void bar(int x)
 {
      printf( "%d\n", x );
 }

 int main()
 {

        void (*foo)(int) = &bar;
        foo(10); //This prints 10.

        return 0; 
}

Upvotes: 1

haccks
haccks

Reputation: 106012

There is no assignment in

void (*pbindRemoveDev)( zAddrType_t *Addr ) = (void*)NULL;

It is initializing function pointer pbindRemoveDev to NULL. Following is the assignment

void RemoveDev( zAddrType_t *Addr );
pbindRemoveDev = RemoveDev // Assignment

Upvotes: 5

ElderBug
ElderBug

Reputation: 6145

This is exactly like :

int a;

But here the type is :

void (*)(zAddrType_t*)

So yes, you can do int a = 0;

Upvotes: 2

Related Questions