I have a function pointer to calculate addition, substraction, etc for 2 inputed arguments and 1 inputed operation. It works fine when the code return the indexed function pointer (with the inputed index) function pointer with both inputed arguments. but when I initialize a variable with as value this indexed f.p. i. , the result is an address, when I try to dereference this i.f.p. the result is -11.
I don't understand why. Can someone explain me why?

 #include <stdio.h>

//Compiler version gcc  6.3.0
/*On declare 4 fonctions*/
int ad (int num1, int num2);
int sub (int num1, int num2);
int mul (int num1, int num2);
int div (int num1, int num2);

int main()
{
     int chif, num1, num2;
     int(*op[4])(int num1, int num2);
     op[0]=ad;
     op[1]=sub;
     op[2]=mul;
     op[3]=div;

     printf("Choisissez deux nombres separes par enter\n");
     scanf("%d%d", &num1, &num2);
     printf("choisissez un chiffre entre 0 et 3 pour +-*/ \n");
     scanf("%d", &chif);

     printf("le reultat est %d\n", op[chif](num1, num2));
     return 0;
}

int ad (int x, int y)
{
     return (x+y);
}
int sub (int x, int y)
{
     return(x-y);
}
int mul (int x, int y)
{
     return(x*y);
}
int div (int x, int y)
{
     return(x/y);
}

/*
Outputs
Choisissez deux nombres separes par enter
1
2
choisissez un chiffre entre 0 et 3 pour +-*/ 
0
le reultat est 3

Process finished.
*/
#include <stdio.h>

//Compiler version gcc  6.3.0
/*On declare 4 fonctions*/
int ad (int num1, int num2);
int sub (int num1, int num2);
int mul (int num1, int num2);
int div (int num1, int num2);

int main()
{
     int chif, num1, num2;
     int(*op[4])(int num1, int num2);
     op[0]=ad;
     op[1]=sub;
     op[2]=mul;
     op[3]=div;
      int result=op[chif](num1, num2);

     printf("Choisissez deux nombres separes par enter\n");
     scanf("%d%d", &num1, &num2);
     printf("choisissez un chiffre entre 0 et 3 pour +-*/ \n");
     scanf("%d", &chif);

     printf("le reultat est %d\n", result);
     return 0;
}

int ad (int x, int y)
{
     return (x+y);
}
int sub (int x, int y)
{
     return(x-y);
}
int mul (int x, int y)
{
     return(x*y);
}
int div (int x, int y)
{
     return(x/y);
}
/*Outputs
Choisissez deux nombres separes par enter
1
2
choisissez un chiffre entre 0 et 3 pour +-*/ 
0
le reultat est -1931796686

Process finished.
*/

I don't know why, but if I write int result=op[chif](num1, num2); after the last scanf it's works fine.
I don't understand why the place in which this statement is writen is relevant. E.g. in Javascript, if I remember right, the place is not relevant. May be that untill the inputs are filled, result is not an integer but a pointer. Anyways, I wrote this as an answer because I cannot edit my question.

Before the scanf functions store values in the variables they could contain anything. So evaluating the result before these variables are set should give you random looking results. Always initialize variable at the place they are declared. This would then give you a better clue as to what is happening.

In line number 18 of second program try to print the value of chif, do you see any one of 0,1,2,3?
its random or unpredictable, so you need to initialize chif before using.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.