Amazon Interview Question
Software Engineer / DevelopersCountry: United States
#include<stdio.h>
typedef int (*simpleCompute) (int, int);
typedef struct{
simpleCompute fun;
} functionStruct;
int sum(int a, int b){
return a+b;
}
int prod(int a, int b){
return a*b;
}
void main(int args[])
{
printf("hello world\n");
functionStruct* ptr;
functionStruct obj1;
functionStruct obj2;
obj1.fun = sum;
obj2.fun = prod;
ptr = &obj1;
printf("caling fun using first assignment: %d\n", ptr->fun(2, 4));
ptr = &obj2;
printf("caling fun using first assignment: %d\n", ptr->fun(2, 4));
}
I think using function pointers, Please correct me if i am wrong!
- ramvak October 19, 2014