Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this declaration typedef void foo(); mean? [closed]

I don't understand the meaning of typedef void interrupt_handler();. Could someone explain it with some examples?

typedef void interrupt_handler();
like image 817
Lefteris Sarantaris Avatar asked Dec 14 '14 23:12

Lefteris Sarantaris


People also ask

What does the declaration void * foo () mean?

As a return “type”. In the function declaration void foo(int) , the void signifies that “ foo does not return a value”.

What is typedef void?

typedef void (*MCB)(void); This is one of the areas where there is a significant difference between C, which does not - yet - require all functions to be prototyped before being defined or used, and C++, which does.

What is typedef function?

typedef is a reserved keyword in the programming languages C and C++. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type.

Which is the correct method to typedef a function pointer?

typedef is a language construct that associates a name to a type. myinteger i; // is equivalent to int i; mystring s; // is the same as char *s; myfunc f; // compile equally as void (*f)(); As you can see, you could just replace the typedefed name with its definition given above.


1 Answers

It means that interrupt_handler is type synonym for function, that returns void and does not specify its parameters (so called old-style declaration). See following example, where foo_ptr is used as function pointer (this is special case where parentheses are not needed):

#include <stdio.h>

typedef void interrupt_handler();

void foo()
{
    printf("foo\n");
}

int main(void)
{
    void (*foo_ptr_ordinary)() = foo;
    interrupt_handler *foo_ptr = foo; // no need for parantheses

    foo_ptr_ordinary();
    foo_ptr();

    return 0;
}
like image 57
Grzegorz Szpetkowski Avatar answered Oct 23 '22 06:10

Grzegorz Szpetkowski