• Announcements

Archived

This topic is now archived and is closed to further replies.

function pointer arrays, or gotos

Recommended Posts

is there a way to get a function pointer array? or: int blah = 1; goto blah; ... ???? would be really interesting for a scripting language

Share on other sites
Sure, you can create an array of function pointers, but you can''t use goto, you would call the functions like normal (parentheses). Also, all of the functions would have to have the same type (return type and arguments). For example:

  // 3 functionsvoid* func1(void* arg) {}void* func2(void* arg) {}void* func3(void* arg) {}typedef void*(*FUNC_PTR)(void*); // A void* func(void*) pointerFUNC_PTR ptrlist[3] = {func1, func2, func3}; // The arrayint main(){  ptrlist[0](NULL);  // Call the first function  ptrlist[2](NULL);  // Call the third function}

I haven''t actually tested this though, so it might not even work. You can''t have dynamic gotos, since the labels are a compile-time thing.

Share on other sites
its not possible to call line numbers, since they are resolved at compile time. a typical line actually expands to many assembly lines, so using goto on a varialbe would be pretty pointless.

Share on other sites
You can only goto a label (look in your C ref book). A goto is translated directly into a jump/branch/etc. They are useful in some situations since using a function pointer involves an indirect jump, but always wind producing messy code. lex and yacc use them, but those are code generation programs.

Share on other sites
Zeusel: Use FORTRAN, it has computed gotos.

[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...

Share on other sites
quote:
Original post by Fruny
Zeusel: Use FORTRAN, it has computed goto s.

Yeah.. And unbeliveably beautiful syntax....

c F77 HelloWorld code      program helloworld      write (*,*) ''Hello world''      stop      end

Share on other sites
GCC has support for slightly crazier goto stuff:

label:
blah;
blah;

someVariableType place = &&label;
goto place;

-- John

Share on other sites
quote:
Original post by EvilCrap
its not possible to call line numbers, since they are resolved at compile time. a typical line actually expands to many assembly lines, so using goto on a varialbe would be pretty pointless.

can''t you use pointer arithmetic to get around this? Although you would have to know the structure of the function to pull it off

Share on other sites
quote:
Original post by Ecko
can''t you use pointer arithmetic to get around this? Although you would have to know the structure of the function to pull it off

Why would you even want to contemplate such an abomination? Forget about arbitrary jumps around the code, and use what the language provides.

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]

Share on other sites
thanks, no fortan but the function pointer stuff is the right thing! i knew about the goto stuff but i thought there would be a way around using asm...

• Forum Statistics

• Total Topics
628281
• Total Posts
2981797

• 10
• 11
• 17
• 14
• 9