Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typedef function vs function pointer

I'm trying to understand function declaration using typedefs.

What does this code do in C++?

typedef void fcn_t(void);
typedef void (*ptr_t)(void);

fcn_t f;
fcn_t *pf;
ptr_t pf2;

In my understanding:

  • fcn_t is the type of a function, and so the line with f is a function declaration (not a definition), and I could later define it like void f(void) { blabla(); bleble(); } just as if I had declared void f(void); instead of fcn_t f;;
  • fcn_t * is the type of a function pointer, and the line with pf is a pointer variable definition, and pf is default-initialized (assuming the code excerpt is from the global scope);
  • There is no difference between fcn_t* and ptr_t, thus everything I said about pf applies to pf2.

Did I get it right? Would any of the three declarations have its meaning changed if I marked them extern? What would change if the code was compiled as C instead of as C++?

like image 482
fonini Avatar asked May 24 '17 03:05

fonini


People also ask

Can we use typedef for function pointer?

A typedef, or a function-type alias, helps to define pointers to executable code within memory. Simply put, a typedef can be used as a pointer that references a function.

What is the major difference between function and function pointer?

Originally Answered: What is the difference between 'function pointer' and 'pointer to a function' ? A pointer to a function is a pointer that points to a function. A function pointer is a pointer that either has an indeterminate value, or has a null pointer value, or points to a function.

Which is the correct method to typedef a function pointer?

h> void upton(int n) { for (int i = 1; i <= n; ++i) printf("%d\n", i); } void nth(int n) { printf("%d\n, n); } Notice they both are having similar signature. So we can create function pointer which would be able to point both the functions . It can be done by the method given below.

Can we typedef a function in C?

The typedef is a keyword used in C programming to provide some meaningful names to the already existing variable in the C program. It behaves similarly as we define the alias for the commands. In short, we can say that this keyword is used to redefine the name of an already existing variable.


1 Answers

Yes you are right on all three counts. The only thing that would change if you marked them extern are the function pointers. Function declarations are by default extern in C++.

Try and compile the following program

template <typename...>
struct WhichType;

typedef void fcn_t(void);
typedef void (*ptr_t)(void);

// constexpr auto one = WhichType<fcn_t>{};
// constexpr auto two = WhichType<fcn_t*>{};
// constexpr auto three = WhichType<ptr_t>{};

fcn_t f;

void f() {}

int main() {
    f();
}

Uncommenting the commented lines will likely give you a compiler error that tells you what types the WhichType instance is being instantiated with, and as a result it should show you the exact types of all three things you asked about. It's a trick I picked up from Scott Meyers' book "Effective Modern C++".


To test whether the declarations are extern or not, write two simple implementation files, with one containing the definition of the variable

main.cpp

template <typename...>
struct WhichType;

typedef void fcn_t(void);
typedef void (*ptr_t)(void);

fcn_t f;

int main() {
    f();
}

definition.cpp

void f() {}

and then compile, link and run definition.cpp and main.cpp (via g++ -std=c++14 definition.cpp main.cpp and then ./a.out). If the declaration was not extern then the compile would fail with an undefined symbol error.

like image 131
Curious Avatar answered Sep 27 '22 20:09

Curious