Redefining how a function operates? C

Asked

Viewed 37 times

0

Hello, I have a question regarding a line of code below, see:

#include <stdio.h>

typedef float (*TPonteiroFuncao)(float, float); // O QUE ESSA LINHA FAZ?

float soma(float a, float b){
    return a+b;
}

float produto(float a, float b){
    return a*b;
}

int main(){
    TPonteiroFuncao pfs,pfp;
    float r; 

    pfs = &soma;
    pfp = &produto;
    r = pfs(12,14);
    printf("Resultado: %.2f\n", r);
    r = pfp(12,14);
    printf("Resultado: %.2f\n", r);

    return 0;
}

Is the line in question redefining how the functions created with the float type will operate? Or it would just be creating a name, so that every time I need a pointer float just type TPonteiroFuncao?

What is the need for (float, float); and what he does?

Thank you!

2 answers

2


TPonteiroFuncao is a typedef, ie, sets a name/nickname for a type, in your case a function/pointer type for function. A type definition for function is marked by its signature, which is composed of the input and return parameters of a function.

So: typedef float (*TPonteiroFuncao)(float, float); defines that to be of the type TPonteiroFuncao, the function must have return float, and two input parameters also of type float, the (float, float) that you asked.

Below we have a clearer example:

#include <stdio.h>

typedef float (*TPonteiroFuncao)(float, float); // O QUE ESSA LINHA FAZ?

float soma(float a, float b){
    return a+b;
}

float produto(float a, float b){
    return a*b;
}

int main(){
    int entrada;
    TPonteiroFuncao acao;
    float resultado; 

    printf("0 - produto;1 - soma\n");
    scanf("%d", &entrada);

    if (entrada)
        acao = soma;
    else
        acao = produto;

    resultado = acao(12, 14);
    printf("Resultado: %.2f\n", resultado);

So far, I think you’ve probably understood more or less how it works, so let’s go to a negative case, a function that doesn’t contain that signature.

So suppose I want another operation, the square of the number:

float quadrado(float x) {
    return x * x;
}

And I define in my doorway:

if (entrada == 2)
    acao = quadrado;
else ...

I get a message from the compiler: warning: incompatible pointer types assigning to 'TPonteiroFuncao' (aka 'float (*)(float, float)') from 'float (float)' because the function’s signature differs from that defined in TPonteiroFuncao.

1

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)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.