0

The pointers function declarations in the following snippets of code allows me to vary the names of each function inside the struct, correct? This is merely hypothetical, so the names are of no consequence yet.

Header File

typedef struct
{
    bool (*enabled)(uint32_void);
    void (*start)(uint32_t);
    void (*stop)(uint32_t);
    bool (*expired)(void);
} battle_star_gallactica;

C File

static bool Battle_v0_enabled (void) { ... }
static void Battle_v0_start (uint32_t) { ... }
static void Battle_v0_stop (uint32_t) { ... }
static bool Battle_v0_stop (void) { ... }

const battle_star_gallactica battle_v0 =
{
    enabled,
    start,
    stop,
    expired
};
...

One Example Use

battle_v0.start(1000);

The C file snippet would be repeated for other versions (e.g. v1 or v2 instead of v0).

How does this work?

EDIT

Corrected code:

Header File

typedef struct
{
    bool (*enabled)(uint32_void);
    void (*start)(uint32_t);
    void (*stop)(uint32_t);
    bool (*expired)(void);
} battle_star_gallactica;

extern const battle_star_gallactica battle_v0;
extern const battle_star_gallactica battle_v1;
...

C File

static bool Battle_v0_enabled (void) { ... }
static void Battle_v0_start (uint32_t val) { ... }
static void Battle_v0_stop (uint32_t val) { ... }
static bool Battle_v0_expired (void) { ... }

const battle_star_gallactica battle_v0 =
{
    Battle_v0_enabled,
    Battle_v0_start,
    Battle_v0_stop,
    Battle_v0_expired
};
...

One Example Use

battle_v0.start(1000);
3
  • "So say we all" -- meaning? Commented Jan 27, 2013 at 0:54
  • 1
    @Biff - You've watched the new "Battlestar Galactica" series, right? Commented Jan 27, 2013 at 1:45
  • Haha, no. I heard it on "The Office". Good to know. Commented Jan 27, 2013 at 2:03

3 Answers 3

2

Yes. But you have a few issues:

1) Battle_v0_stop is defined twice 2) When initing battle_v0 you need to prefix values with Battle_v0_

You could then do:

battle_v0.enabled()

But I would have currnet battle star galactic and use it:

const battle_star_gallactica bsg = battle_v0;
bgs.enabled()
Sign up to request clarification or add additional context in comments.

5 Comments

Is there a name for the manner in which the pointers were used? I was going to read more about it in a C book.
I simply call them function pointers. see newty.de/fpt/index.html and en.wikipedia.org/wiki/Function_pointer
Do the names of the pointers even matter? Could I, for instance, only have void (*)(uint32_t) for the first function pointer?
You do need a name for fucntion pointer. How else would you reference it?
You should not have changed the code that you posted. Now other readers will not be able to understand my answer.
1

You have no functions named enabled, start, and so on. When initializing an object with function pointers, pass a pointer to the actual function name (i.e. Battle_v0_start). That said, it is important to notice that your types do not match. It should be Battle_v0_start(uint32_t), not void.

But yes, after being properly initialized, you will be able to simply call your_obj.your_struct_function_name(your_arguments, ...). In your case battle_v0.start(1000) will call Battle_v0_start (if you pass this function to the pointer). You can think of this like most other pointer types.

Comments

1

You have to assign the target function to each of your function pointers, such as

battle_v0.enabled = Battle_V0_enabled;
battle_v0.start   = Battle_V0_start;
// etc.

or

const battle_star_galactica battle_v0 = {
    Battle_V0_enabled,
    Battle_V0_start,
    Battle_V0_stop,
    Battle_V0_expired
};

Then you can call the functions through the pointer:

battle_v0.start(1000);  // or battle_v0.(*start)(1000)

will call the Battle_V0_start function with a value of 1000.

Note that the function signatures (return type and number/types of arguments) have to match; for example, you declare the function pointer for start to take an argument of type uint32_t, but Battle_V0_start takes no arguments. One or the other of these needs to change so that they match.

void Battle_V0_start(uint32_t val) { ... }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.