• Advertisement
Sign in to follow this  

function pointers and er...

This topic is 3025 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can something like this be done somehow in C? int a,b,c;
void (*Function)(void);

Function = {a=5;b=8;c=some_function(a,b);}

Share this post


Link to post
Share on other sites
Advertisement
Assuming you're asking about inside a function definition, then no, C doesn't support nested function definitions.

Share this post


Link to post
Share on other sites
No, but you can do something like this however


void MyFunction()
{
a=5;
b=8;
c=some_function(a,b);
}

void (*Function)(void);

Function = &MyFunction;

Share this post


Link to post
Share on other sites

#include <stdio.h>

typedef int (Fn)(int, int);

typedef struct {
Fn *fn;
int a, b;
} BoundFn;

int callBoundFn(BoundFn c) {
return c.fn(c.a, c.b);
}

int add(int x, int y) {
return x + y;
}

int main() {
BoundFn function = {add, 5, 8};
int c = callBoundFn(function);

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

return 0;
}


? :)

But really, the fact that you're asking this question suggests that something is iffy with your design. What are you trying to do?

Share this post


Link to post
Share on other sites
Don't talk to me about iffy design.
I'm a tinker programmer, so all my designs are iffy/tinker as hell.

BTW I'm building a menu/gui system, and I want want to add custom functions to the items.
I asked the question, because I'm simply lazy, and didn't want to declare (I don't even know if it's the right term) these functions one by one.

The idea came from MATLAB BTW.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement