8
Why do you ask:
The question may seem strange, but is that I am a beginner in programming and at this moment I am studying pointers to function and in an example in the book by which study is presented the possibility of creating pointers to function and storing them in a array, among one of its purposes would be the creation of a system controlled by a menu of options.
Code:
Note: I will not put all the code here, but only what I think is enough to elaborate the question. If the reader finds it necessary, the full example code taken from the book is at the end of the text.
First an array of function pointers of the void type is defined that take an integer type argument:
void (*f[ 3 ])( int ) = { function1, function2, function3 };
Through a prompt the user informs an integer that is stored in a variable with identifier Choice. This same variable is used in the function pointer array subscript:
(*f[ choice ])( choice );
The function contained in the position Choice of array is executed with the assignment of an integer parameter. Hence my question:
Question:
It is a very theoretical question, indeed, but I would like to understand how the compiler C recognizes the space in bytes that must travel in memory to access another function, because as far as I know, the subscript of a array in C is just an operator that adds an integer following a pointer arithmetic - If you obey that logic, in the example presented should be covered (**choices * qtd. bytes de uma função**)
.
C: how to program / Paul Deitel, Harvey Deitel ; -- 6. ed, 2011. p. 269.
/* Fig. 7.28: fig07_28.c
Demonstrando um array de ponteiros para funções */
#include <stdio.h>
/* protótipos */
void function1( int a );
void function2( int b );
void function3( int c );
int main( void )
{
/* inicializa array de 3 ponteiros para funções que usam um
argumento int e retornam void */
void (*f[ 3 ])( int ) = { function1, function2, function3 };
int choice; /* variável para manter escolha do usuário */
printf( "Digite um número entre 0 e 2, 3 para sair: " );
scanf( "%d", &choice );
/* processa escolha do usuário */
while ( choice >= 0 && choice < 3 ) {
/* chama a função para o local selecionado do array f e passa
choice como argumento */
(*f[ choice ])( choice );
printf( "Digite um número entre 0 e 2, 3 para terminar: ");
scanf( “%d”, &choice );
} /* fim do while */
printf( "Execução do programa concluída.\n" );
return 0; /* indica conclusão bem-sucedida */
} /* fim do main */
void function1( int a )
{
printf( "Você digitou %d, de modo que function1 foi chamada\n\n", a );
} /* fim de function1 */
void function2( int b )
{
printf( "Você digitou %d, de modo que function2 foi chamada\n\n", b );
} /* fim de function2 */
void function3( int c )
{
printf( "Você digitou %d, de modo que function3 foi chamada\n\n", c );
} /* fim de function3 */
I’m new to Stackoverflow too, when I went to add a title I pressed ENTER hoping to add a line break, but I ended up publishing the question. So I just wanted to finish: Does the compiler really recognize the size of the function in memory? Or, in another hypothesis, what the compiler recognizes are pointers with an undefined compile-time size?
– joaov_machado