Archived

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

Zeusel

function pointer arrays, or gotos

Recommended Posts

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 functions

void* func1(void* arg) {}
void* func2(void* arg) {}
void* func3(void* arg) {}

typedef void*(*FUNC_PTR)(void*); // A void* func(void*) pointer


FUNC_PTR ptrlist[3] = {func1, func2, func3}; // The array


int 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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
GCC has support for slightly crazier goto stuff:

label:
blah;
blah;

someVariableType place = &&label;
goto place;

-- John

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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...

Share this post


Link to post
Share on other sites