Thread: Pointers to function(function pointers)

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    1

    Question Pointers to function(function pointers)

    dear freinds i have two problems.
    1) Pointers to functions (Function Pointers)
    i don't know what is the error in the following program....

    #include<stdio.h>
    #include<string.h>
    void check(char *a, char *b, int(*cmp)());
    main()
    {

    void check(char *a, char *b, int(*cmp)());
    char s1[80],s2[80];
    int (*p)();
    p=strcmp;
    gets(s1);
    gets(s2);
    check(s1,s2,p);
    }

    void check(char *a, char *b,int(*cmp)())
    {
    printf("testing for equality \n");
    if (!(*cmp)(a,b))
    printf("equal");
    else
    printf("not equal");
    }

    2) how to pass structures as an argument to a functiion e.g

    intcal (struct {struct{ char name[10];
    int num;
    float amt;}bill)..
    ...
    ..
    .....
    etc..

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (!(*cmp)(a,b))
    Try

    if ( cmp(a,b) == 0 )

    > 2) how to pass structures as an argument to a functiion
    You declare the structure outside the function prototype

    Code:
    struct foo { 
        char name[10];
    };
    
    void myfn ( struct foo var );

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    9
    Hello,

    For problem 1,

    You declared a function pointer cmp like this

    int (*cmp)()

    which means that this function takes no arguments and just returns an int. What you should be doing infact is this,

    int (*cmp)(char *, char *)

    which says that cmp is a ptr-to-a-func that takes two char*'s as its arguments and returns an int value.


    For problem 2,

    C doesn't support passing structures like you specified in your code yet. You should create a variable which holds the required values and then pass 1. either the structure itself, or 2. a pointer to it.

    for ex:

    typedef struct {
    int x,
    y;
    } point;

    void print(point *p) {
    printf("x is %d, y is %d", p->x, p->y);
    }

    int main() {
    point p = {1, 2};
    print( &p );

    return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > which means that this function takes no arguments
    Actually, it means that the arguments are unspecified

    func() is the equivalent of func(...) in C
    func() is the equivalent of func(void) in C++

    But it would be better written as you suggest anyway


    > C doesn't support passing structures like you specified in your code yet
    ANSI-C has allowed structures to be passed as parameters for over a decade now.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    9

    Unhappy

    Hello all,


    The big-wigs should excuse this ignorant soul, I thought one couldn't pass structures like this
    Code:
    void SomeFn( struct { int a;} b ) { //code }
    I think I also made another mistake in my earlier post. The variable "cmp" was declared like this
    Code:
    int (*cmp)(char *, char *) = strcmp;
    Instead it should be
    Code:
    int (*cmp)(const char *, const char *) = strcmp;
    This I think should work just fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM