So here's what I'm working with.
{
int Test;
void (*functions[5])() = {
return,
one,
two,
three,
four
};
for (Test = takeInput(5);; Test = takeInput(5)){
cls();
Print(L"0) Abort\n");
Print(L"1) One\n");
Print(L"2) Two\n");
Print(L"3) Three\n");
Print(L"4) Four\n");
Print(L"Enter Selection: ");
(*functions[Test])();
}
}
That's the gist of what I'm trying to do. This is sort of a menu system for some context (and takeInput(5), will pause for input, stopping the loop until someone inputs a number).
What the problem is, is that using return in that way is apparently wrong. I figured that seeing as how (i thought) return is a function, i can't see why it wouldn't be able to be put into a function pointer array in order to break out of the menu. I suppose I could make a dummy function that basically just returns true when called, and check for that (the problem with that being that all the other functions return void). Even if that worked, I would rather have a solution that is as self contained as the above, non-working example.
Is there a nice, clean, hopefully short way of doing this that is better than anything I've thought of yet?
return
is not a function, it has no address and therefore you can't have it in the array of pointers to functions.
Further, Test
appears uninitialized when you use it the first couple of times:
for (Test;
and
(*functions[Test])();
If you want to break out of the for
loop, you have to use either break
or return
or an appropriate condition between the two semicolons in for(;condition;)
.
A function call will not break the loop. Unless, it's something exotic like abort()
, exit()
or longjmp()
.
我将用NULL
替换函数数组中的return
并在执行函数之前测试null,当函数指针为NULL
时返回。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.