GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Monday, May 30, 2011

Function Pointer As Struct Member

Function pointer as struct member - with arguments:

#include <stdio.h>

struct aritmathic{
    int a;
    int b;
    int (*add)(int, int);
    int (*subtract)(int, int);
    int (*multiply)(int, int);
};

int Add(int a, int b){
    return (a+b);
}

int Subtract(int a, int b){
    return (a-b);
}

int Multiply(int a, int b){
    return (a*b);
}

int main(){

    struct aritmathic ar;
    ar.a = 4;
    ar.b = 5;

    ar.add = Add;
    ar.subtract = Subtract;
    ar.multiply = Multiply;

    int a = ar.add(ar.a, ar.b);
    int b = ar.subtract(ar.a, ar.b);
    int c = ar.multiply(ar.a, ar.b);   

    printf("%d\n%d\n%d", a, b, c);

    return 0;
}


Simpler version for above:
#include <stdio.h>

struct aritmathic{
    int a;
    int b;
    int (*add) (struct aritmathic*);
    int (*subtract) (struct aritmathic*);
    int (*multiply) (struct aritmathic*);
};

int Add(struct aritmathic* ar){
    return (ar->a + ar->b);
}

int Subtract(struct aritmathic* ar){
    return (ar->a - ar->b);
}

int Multiply(struct aritmathic* ar){
    return (ar->a * ar->b);
}

int main(){

    struct aritmathic ar;
    ar.a = 4;
    ar.b = 5;

    ar.add = Add;
    ar.subtract = Subtract;
    ar.multiply = Multiply;

    int a = ar.add(&ar);
    int b = ar.subtract(&ar);
    int c = ar.multiply(&ar);   

    printf("%d\n%d\n%d", a, b, c);

    return 0;
}

Share/Bookmark

4 comments:

  1. mas ijin nyuwun backline kita sama sama untung klik di sini

    ReplyDelete
  2. You forgot to add the last \n in your printf statements

    ReplyDelete
  3. In this fashion my partner Wesley Virgin's tale begins in this shocking and controversial VIDEO.

    You see, Wesley was in the army-and shortly after leaving-he discovered hidden, "SELF MIND CONTROL" secrets that the government and others used to get everything they want.

    As it turns out, these are the same secrets many celebrities (notably those who "became famous out of nowhere") and the greatest business people used to become rich and famous.

    You probably know how you utilize only 10% of your brain.

    Really, that's because most of your brainpower is UNCONSCIOUS.

    Maybe that conversation has even taken place INSIDE OF YOUR very own head... as it did in my good friend Wesley Virgin's head around seven years ago, while riding an unlicensed, beat-up garbage bucket of a vehicle without a license and with $3 on his banking card.

    "I'm absolutely fed up with living check to check! When will I get my big break?"

    You've been a part of those those conversations, ain't it so?

    Your success story is going to happen. You just need to take a leap of faith in YOURSELF.

    Learn How To Become A MILLIONAIRE Fast

    ReplyDelete