Thread: Naming pointer to function in a struct...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    752

    Exclamation Naming pointer to function in a struct...

    Okay, I'm writing a program where I need to use structs which contain two pointers to functions. I want to refer to these pointers as choose and decide... I'd like to know now if this code is the right way...
    Code:
    struct
    {
     int ident;
     int (*choose) (int);
     int (*decide) (int);
    } personality[NUM];
    And is there any other way? Like maybe...
    Code:
    struct
    {
     int ident;
     int (*) (int) choose;
     int (*) (int) decide;
    } personality[NUM]
    Also, I'd like to make sure that the following is a valid use of these function pointers.
    Code:
    j = (*personality[i].choose)(i);
    I've never really used something like this before so... help me save a little time here anyone who already knows what the proper syntax is?
    Callou collei we'll code the way
    Of prime numbers and pings!

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    And oh yea, is there any way to name the parameters of the function.. you know, just for documentation? Like...
    Code:
    struct
    {
     int ident;
     int (*choose) (int self);
     int (*decide) (int judgeMe);
    } personality[NUM];
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Yes, the first one is the right way. However, you can make it cleaner and easier to understand with a typedef:

    ( hope it's correct, I don't have a compiler here... )

    PHP Code:

    #include <stdio.h>

    typedef int (*FUNCTION_POINTER_INT2INT)(int);

    const 
    unsigned int NUM 2;

    struct
    {
        
    int ident;
        
    FUNCTION_POINTER_INT2INT choose;
        
    FUNCTION_POINTER_INT2INT decide;
    personality[NUM];

    int testint i )
    {
        return 
    i*2;
    }

    int main()
    {
        
    personality[0].choose test;

        
    printf"%d"personality[0].choose) );

        return 
    0;

    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Ehhe, okay, now I have another problem...
    Code:
    enum {kind, usurper, egoist, NUM};
    
    struct
    {
     int ident;
     int (*choose) (void);
     int (*decide) (int);
    } personality[NUM];
    
    int kind_choose (void);
    int kind_decide (int judgeMe);
    
    personality[kind] = {kind, kind_choose, kind_decide};
    Let me be the first to say that this did not work.

    I'm sure there's other problems with this, but the first problem is that
    personality[kind] = {kind, kind_choose, kind_decide};
    looks like a declaration of an array of kind elements, not the assignment of values struct personality[kind] (admittedly, this probably isn't even a legal way to assign values to a struct).

    This leaves me in kind of a problem, because I would like a nice and easy way to add and remove personalities. I considered using something like this...
    Code:
    #define ASSIGNPERSONALITY(A) \\
    { personality[A].ident = A; \\
      personality[A].choose = A ## _choose; \\
      personality[A].decide = A ## _decide; }
    But at first I'm not even sure this will work, since each A is going to be an enum constant, and I think that this is a pretty confusing construct anyhow.

    So anyways, does anyone have a suggestion for a good way for me to -easily- add/remove personalities when I compile?
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The only thing wrong with your last example is failure to include 'struct' before it, that and you haven't named your structure. I'd do something like:
    Code:
    struct personalityTypes {
        int ident;
        int (*chose)(void);
        int (*decide)(int);
    };
    Then build a table, I think you could use your macro, but let me clarify, you want a compile-time list change, not a runtime? A compile time change could use something like:

    Non-macro version:
    Code:
    struct personalityTypes personalityTable[] =
    {
        { kind, kind_choose, kind_decide },
        { usurper, usurper_choose, usurper_decide },
        ... more ... 
    };
    Or, with slight modification, your macro might work, I personally haven't tried, but I believe it should do the trick:

    Macro version:

    Code:
    #define PERSONALITY(A) { A, A ## _chose, A ## _decide }
    
    struct personalityTypes personalityTable[] =
    {
        PERSONALITY(kind),
        PERSONALITY(usurper),
        PERSONALITY(egoist)
    }
    That should do the trick.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM