Function pointers uasage

Posted by chaitanyavarma on Stack Overflow See other posts from Stack Overflow or by chaitanyavarma
Published on 2010-06-14T15:16:26Z Indexed on 2010/06/14 15:22 UTC
Read the original article Hit count: 145

Filed under:
|

Hi All, Why these two codes give the same output, Case - 1:

#include <stdio.h>

typedef void (*mycall) (int a ,int b);
void addme(int a,int b);
void mulme(int a,int b);
void subme(int a,int b);

main()
{
    mycall x[10];
    x[0] = &addme;
    x[1] = &subme;
    x[2] = &mulme;
    (x[0])(5,2);
    (x[1])(5,2);
    (x[2])(5,2);
}

void addme(int a, int b) {
    printf("the value is %d\n",(a+b));
}
void mulme(int a, int b) {
    printf("the value is %d\n",(a*b));
}
void subme(int a, int b) {
    printf("the value is %d\n",(a-b));
}

Output:

the value is 7
the value is 3
the value is 10

Case -2 :

#include <stdio.h>

typedef void (*mycall) (int a ,int b);
void addme(int a,int b);
void mulme(int a,int b);
void subme(int a,int b);

main()
{
    mycall x[10];
    x[0] = &addme;
    x[1] = &subme;
    x[2] = &mulme;
    (*x[0])(5,2);
    (*x[1])(5,2);
    (*x[2])(5,2);
}

void addme(int a, int b) {
    printf("the value is %d\n",(a+b));
}
void mulme(int a, int b) {
    printf("the value is %d\n",(a*b));
}
void subme(int a, int b) {
    printf("the value is %d\n",(a-b));
}

Output:

the value is 7
the value is 3
the value is 10

© Stack Overflow or respective owner

Related posts about c++

Related posts about function-pointers