Sign in to follow this  
szecs

function pointers and er...

Recommended Posts

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this