The keyword typedef
is intended to associate a name to a guy.
It is a widely used language construct to simplify the declaration syntax of complex data structures, providing more descriptive (humanly readable) names for the types, see only:
/* Inteiro */
typedef int INTEIRO;
/* Ponteiro para char */
typedef char* PSTRING;
/* Array de chars */
typedef char CPF[11+1];
/* Estrutura */
typedef struct carro_s { char * modelo; char * placa; } CARRO;
/* Ponteiro para função */
typedef void (*FuncXPTO)(int,int);
Enabling the following statements:
INTEIRO i;
PSTRING p;
CPF cpf;
CARRO c;
FuncXPTO f;
The line in question is redefining as the functions created with the type
float will operate? Or would it just be creating a name, so that every time I need a float pointer just type Tponteirofuncao?
What is the need for (float, float); and what it does?
The line in question is only one most readable name established to simplify the declaration of a pointer to a function which in turn receives two values floats
in its list of arguments and returns a value float
, for example:
float foobar( float a, float b )
{
return( a * b );
}
Just see how your program could make the most of typedef
and pointers for function:
#include <stdio.h>
typedef float (FuncType)(float, float);
float soma(float a, float b){
return a+b;
}
float produto(float a, float b){
return a*b;
}
float operacao( float a, float b, FuncType * pfunc ) {
return pfunc( a, b );
}
int main(){
float res;
res = operacao( 12, 14, soma );
printf("Soma: %.2f\n", res );
res = operacao( 12, 14, produto );
printf("Multiplicacao: %.2f\n", res );
return 0;
}
Exit:
Soma: 26.00
Multiplicacao: 168.00
See working on Repl.it.
"Any fool can write code that a computer understands. Good programmers write code that humans can understand".
(Martin Fowler)