Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A typedef function as a parameter of another function

Tags:

c

syntax

typedef int (xxx)(int yyy); means define a function which named xxx with an integer parameter. you can see this SO post for details.

I have tried this in different ways, that is my code:

#include<stdio.h>
#include<stdlib.h>
typedef int (xxx)(int yyy);

void f1(xxx a)
{
    printf("f1:%d\n",a);
}
void f2(xxx *a)
{
    printf("f2:%d\n",a);
}
int test(int y)
{
}
int main()
{
    xxx *a;
    f1(test);
    f1(a);
    f2(test);
    f2(a);

    xxx b;
    printf("xxx's size:%d\n", sizeof(b));
}

output:

f1:4199274
f1:2
f2:4199274
f2:2
xxx's size:1

My question:

  1. f(xxx a) is the same as f(xxx *a)?
  2. sizeof(someFunction) is defined or not?
like image 651
Sayakiss Avatar asked Aug 27 '13 04:08

Sayakiss


People also ask

Can typedef be in function?

You can create a typedef for a function definition, then use that typedef as the parameter to another function. Then any function that matches that typedef can be used.

What is the use of 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.

Does typedef have scope?

A typedef declaration introduces a name that, within its scope, becomes a synonym for the type given by the type-declaration portion of the declaration. You can use typedef declarations to construct shorter or more meaningful names for types already defined by the language or for types that you've declared.


1 Answers

means define a function which named xxx

It defines a function type.

f(xxx a) is the same as f(xxx *a)?

Yes. But this is sort of a hack in the language. The star is just helpfully added for you because it was missing.

sizeof(someFunction) is defined or not?

It specifically is not. You can do this with in assembly language, or by using intimate knowledge of how your program is linked, but it has no semantic meaning to C. Functions don't even need to be addressable beyond indirect jumps, as C supports Harvard architecture machines. (Also, note that two functions with the same type will usually have different sizes, as the size is determined by the number and nature of its instructions.)

C11 6.3.2.1/4 says

A function designator is an expression that has function type. Except when it is the operand of the sizeof operator, … a function designator with type ‘‘function returning type’’ is converted to an expression that has type ‘‘pointer to function returning type’’.

and 6.5.3.4/1 says

The sizeof operator shall not be applied to an expression that has function type…

It appears that your compiler is failing to apply 6.3.2.1/4. An error message is required.

like image 95
Potatoswatter Avatar answered Sep 20 '22 02:09

Potatoswatter