Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Example of writing a funciton pointer in c & c++
07-24-2010, 05:41 PM
Post: #1
Example of writing a funciton pointer in c & c++
Code:
#include <stdio.h>
void my_int_func(int x)
{
    printf( "%d\n", x );
}


int main()
{
    void (*foo)(int);
    foo = &my_int_func;

    /* call my_int_func (note that you do not need to write (*foo)(2) ) */
    foo( 2 );
    /* but if you want to, you may */
    (*foo)( 2 );

    return 0;
}
Visit this user's website Find all posts by this user
Quote this message in a reply
07-24-2010, 07:47 PM
Post: #2
RE: Example of writing a funciton pointer in c & c++
Very good example. Here is my example with 4 basic mathematical operations. It is an example of array of operations :
Code:
#include <stdio.h>
#include <stdlib.h>

double addition(double a, double b);
double substraction(double a, double b);
double multiplication(double a, double b);
double division(double a, double b);

double (*operation[4])(double a,double b);

int main()
{
  operation[0]=&addition;
  operation[1]=&substraction;
  operation[2]=&multiplication;
  operation[3]=&division;
  double m,n;  
  int op;
  printf("Enter two numbers : \n");
  scanf("%lf %lf",&m,&n);
  printf("Enter operation :  \n1 + \n2 - \n3 * \n4 / \n");
  scanf("%d",&op);
  printf("Solution is : %5.2lf ",(*operation[op-1])(m,n));
  system("pause");    
}
double addition(double a, double b)
{
  return (a+b);      
}
double substraction(double a, double b)
{
  return (a-b);      
}
double multiplication(double a, double b)
{
  return (a*b);      
}
double division(double a, double b)
{
  return (a/b);      
}

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
07-25-2010, 06:12 AM
Post: #3
RE: Example of writing a funciton pointer in c & c++
Very good, both of you. I learned quite a bit reading this.

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
07-25-2010, 02:00 PM
Post: #4
RE: Example of writing a funciton pointer in c & c++
Very nice example!

(07-24-2010 07:47 PM)drdebcol Wrote:  Very good example. Here is my example with 4 basic mathematical operations. It is an example of array of operations :
Code:
#include <stdio.h>
#include <stdlib.h>

double addition(double a, double b);
double substraction(double a, double b);
double multiplication(double a, double b);
double division(double a, double b);

double (*operation[4])(double a,double b);

int main()
{
  operation[0]=&addition;
  operation[1]=&substraction;
  operation[2]=&multiplication;
  operation[3]=&division;
  double m,n;  
  int op;
  printf("Enter two numbers : \n");
  scanf("%lf %lf",&m,&n);
  printf("Enter operation :  \n1 + \n2 - \n3 * \n4 / \n");
  scanf("%d",&op);
  printf("Solution is : %5.2lf ",(*operation[op-1])(m,n));
  system("pause");    
}
double addition(double a, double b)
{
  return (a+b);      
}
double substraction(double a, double b)
{
  return (a-b);      
}
double multiplication(double a, double b)
{
  return (a*b);      
}
double division(double a, double b)
{
  return (a/b);      
}
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: