function pointers and er...

Started by
3 comments, last by szecs 14 years, 5 months ago
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);}
Advertisement
Assuming you're asking about inside a function definition, then no, C doesn't support nested function definitions.
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;
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
#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?
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.

This topic is closed to new replies.

Advertisement