user14528515
user14528515

Reputation:

Unable to understand pointers in c in a function

I was reading this code on a website. I am fairly new to programming so please explain in a bit more detail.

#include <stdio.h> 
// A normal function with an int parameter 
// and void return type 
void fun(int a) 
{ 
    printf("Value of a is %d\n", a); 
} 
  
int main() 
{ 
    // fun_ptr is a pointer to function fun()  
    void (*fun_ptr)(int) = &fun; 
  
    /* The above line is equivalent of following two 
       void (*fun_ptr)(int); 
       fun_ptr = &fun;  
    */
  
    // Invoking fun() using fun_ptr 
    (*fun_ptr)(10); 
  
    return 0; 
} 

Doubts-
I am not able to understand this type of declaration and assignment void (*fun_ptr)(int) = &fun;
I mean that if we declare a data type, then we do it like int a; and assign it as a=10; but here we are assigning it by writing (*fun_ptr)(10);. Kindly help.

Upvotes: 0

Views: 280

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

Instead of this record

(*fun_ptr)(10);

you could just write

fun_ptr(10);

That is it is a function call of the function fun pointed to by the function pointer fun_ptr due to the initialization of that pointer in its declaration by the function address

void (*fun_ptr)(int) = &fun;

In turn this declaration could be written simpler like

void (*fun_ptr)(int) = fun;

because a function designator (in this case fun) used in expressions as for example an initializer is implicitly converted to pointer to the function.

You could use a typedef alias for the function type the following way

typedef void Func( int );

In this case the above declaration of the function pointer could look simpler like

Func *fun_ptr = fun;

Here is your program rewritten using a typedef for the function type of the function fun.

#include <stdio.h>

typedef void Func( int );

//  Function declaration without its definition using the typedef
//  This declaration is redundant and used only to demonstrate
//  how a function can be declared using a typedef name
Func fun;

//  Function definition. In this case you may not use the typedef name
void fun( int a )
{
    printf("Value of a is %d\n", a);
}

int main(void) 
{
    //  Declaration of a pointer to function
    Func *fun_ptr = fun;
    
    //  Call of a function using a pointer to it
    fun_ptr( 10 );

    return 0;
}

Upvotes: 1

koder
koder

Reputation: 2103

This is an advanced topic if you are new to programming, fun_ptr is a pointer to a function.

The declaration:

void (*fun_ptr)(int) = fun;

Means fun_ptr is a pointer to a function taking an int argument and returning void, initialize if with a pointer to the function fun. (you don't need the &)

The line:

(*fun_ptr)(10);

does not assign anything, it calls the function pointed to by fun_ptr, but is way to complex

fun_ptr(10);

As fun_ptr points to fun this is equivalant to `fun(10).

Using a function pointer has its use eg in a sort function where the comparison function is passed in as a function pointer so the sorting can be different between calls. achieves the same thing and is much easier on the eyes.

Upvotes: 0

IrAM
IrAM

Reputation: 1738

Following is the meaning of two statments.

void (*fun_ptr)(int) = &fun; this is called declaring and initializing the fun_ptr in the same line , this is same as doing int a = 10;

(*fun_ptr)(10); is not assignment statement, it is invoking the function fun through function pointer fun_ptr.

you can also use typedef to create a new user defined out of function pointer and use as shown in above answer.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409412

Lets rewrite it a little bit, by using type-aliases and some comments:

// Define a type-alias names fun_pointer_type
// This type-alias is defined as a pointer (with the asterisk *) to a function,
// the function takes one int argument and returns no value (void)
typedef void (*fun_pointer_type)(int);

// Use the type-alias to define a variable, and initialize the variable
// This defines the variable fun_ptr being the type fun_pointer_type
// I.e. fun_ptr is a pointer to a function
// Initialize it to make it point to the function fun
fun_pointer_type fun_ptr = &fun;

// Now *call* the function using the function pointer
// First dereference the pointer, to get the function it points to
// Then call the function, passing the single argument 10
(*fun_ptr)(10);

Hopefully it makes things a little clearer what's going on.

Upvotes: 0

Related Questions