17
I wanted to know how the function passed by parameter as it happens in pthread_create (thread,atributo,rotina,argumento);
. On the field rotina
a function is placed in void*
.
17
I wanted to know how the function passed by parameter as it happens in pthread_create (thread,atributo,rotina,argumento);
. On the field rotina
a function is placed in void*
.
30
In C and C++, in addition to normal pointers, it is possible to create pointers for functions. Just as a normal pointer points to a memory region from where you can read or write a value, a pointer to function points to a function, which can be called as a normal function.
//func é uma funão normal
void func(int x) { ... }
//func_ptr é um ponteiro para função void que aceita um int de parâmetro
void (*func_ptr)(int);
//func_ptr passa a apontar para func
func_ptr = &func;
//chama func, através do ponteiro
func_ptr(42);
The use of &
before the function name is optional, usually only the name itself is used.
The syntax for declaring function pointers is not very complicated. Basically it is:
<tipo-de-retorno> ( *<nome-da-variável> ) ( <parâmetros> )
Parentheses are important to indicate that the *
is connected to the variable. Without them would be declared a normal function that returns a pointer.
// ponteiro de função que aceita char* e retorna int
int (*fun_ptr)(char *);
// função que aceita char* e retorna int*
int *fun_ptr(char *);
To make it a little easier, you can use typedefs
// FuncPtr é um ponteiro para funções que recebem e retornam char*
typedef char *(*FuncPtr)(char *);
// Cria um ponteiro de função e atribui a função gets a ele
FunPtr ptr = gets;
In addition to variables such as in the example, it is possible to declare function parameters, as in the case mentioned in the question. Thus it is possible to customize algorithms, passing variable parts of them as a function.
#include <stdio.h>
int soma(int a, int b) { return a + b; }
int multiplica(int a, int b) { return a * b; }
typedef int (*Operacao)(int,int);
//Executa uma operação binária sobre dois operandos
int opera(int a, int b, Operacao op) {
return op(a,b);
}
int main() {
printf("%d\n", opera(3, 4, soma)); //imprime 7
printf("%d\n", opera(3, 4, multiplica)); //imprime 12
}
Browser other questions tagged c pointer anonymous-function
You are not signed in. Login or sign up in order to post.
Answer much better than my answer piggy, I even deleted it :) Congratulations Gesser
– speeder
Quiet, the important thing is that the doubt has been resolved.
– C. E. Gesser
Very complete answer, but maybe it is interesting to show what was asked: how to pass a function as parameter. For example:
void minha_funcao(void *a) { /* ... */ }
followed by:pthread_create(&t, NULL, minha_funcao, NULL);
– ehabkost
A recurring question about pointers to functions is the syntax in the function declaration that receives it as parameter (pthread_create in the case). I think that answer could be edited to include this.
– korbes
Answering requests, reply edited.
– C. E. Gesser
What a spectacular answer! I’ve read some 10 different articles about pointer functions and only after that answer I was able to understand. Congratulations!
– user26323