passing struct member to function

This is a discussion on passing struct member to function within the C Programming forums, part of the General Programming Boards category; I don't know what else to do. For hours I've been trying - but no luck. Here is the problem: ...

  1. #1
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166

    Question passing struct member to function

    I don't know what else to do. For hours I've been trying - but no luck.

    Here is the problem:

    Code:
    typedef int (*fnptr)(double, double);
    
    ...
    
    struct abc
    {
        char check1[3], check2[3], check3[3];
        ...
        fnptr func1, func2, func3;
    }mystruct;
    
    ...
    
    int foo1(double t1, double t2) { ... }
    
    ...
    
    void myfunc(const char a[], fnptr ptr)
    {
        if(strcmp(a, "foo")
            ptr = func1; // gcc error: assignment from incompatible pointer types 
        ....
    }
    
    ...
    
    int main()
    {
        ...
        myfunc(mystruct.check1,  &mystruct.func1);
        ....
        return 0;
    }
    What am I doing wrong? How can I pass the two struct members (check1, func1) to the function myfunc()? I must pass the function pointer 'func1' by reference.

    Thank you for your help.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Is this what you want:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef int (*fnptr) (double, double);
    
    struct abc
    {
        char    check1[30], check2[30], check3[30];
        fnptr   func1, func2, func3;
    } mystruct;
    
    int foo1(double t1, double t2)
    {
        printf("%f %f\n", t1, t2);
        return(1);
    };
    
    int foo_default(double t1, double t2)
    {
        printf("Default function: %f %f\n", t1, t2);
        return(1);
    };
    
    void myfunc(const char a[], fnptr *ptr)
    {
        *ptr = foo_default;
        if (strcmp(a, "foo") == 0) *ptr = foo1;
    }
    
    int main(void)
    {
        strcpy(mystruct.check1, "foo");
        myfunc(mystruct.check1, &mystruct.func1);
        mystruct.func1(1.0, 2.0);
        strcpy(mystruct.check1, "notfoo");
        myfunc(mystruct.check1, &mystruct.func1);
        mystruct.func1(1.0, 2.0);
        return(0);
    }
    
    /*
    Output:
    1.000000 2.000000
    Default function: 1.000000 2.000000
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Thank you very much for your help. Now it's working. I forgot to change the parameter types of the functions I wanted to point to from float to double.
    But your efforts were not in vain. You helped me to avoid another pitfall with this line:

    *ptr = foo_default;

    It would have been quite a mess, had I forgotten to initialize this ptr just in case non of the if ... else if were true. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Passing class member function to pthread_create
    By lehe in forum C++ Programming
    Replies: 6
    Last Post: 03-27-2009, 07:47 PM
  4. problem returning struct member from function
    By yukster in forum C Programming
    Replies: 6
    Last Post: 08-24-2003, 01:21 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21