How i write a menu program in C using a struct instead of a if else or switch statement. #202643
Answered
by
jeallz
winderson0012
asked this question in
Programming Help
-
🏷️ Discussion TypeQuestion BodyThe example below uses a struct to map strings to functions. Credit note: I didn't write this code by hand—it was entirely generated by my Cstrings program.
Guidelines
|
Beta Was this translation helpful? Give feedback.
Answered by
jeallz
Jul 22, 2026
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

This is a nice demonstration of using function pointers to replace large
switchstatements or deeply nestedifchains. I like how the menu entries are data-driven rather than hardcoded into the control flow.That said, I think there are a few improvements that could make the implementation more scalable and easier to maintain.
1. Make the menu size dynamic
Instead of hardcoding the number
4in the loop, compute it automatically. This allows adding new menu entries without modifying the search logic.