Click here to Skip to main content
15,904,346 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: There are many gotos, but these ones are mine Pin
trønderen11-May-24 17:40
trønderen11-May-24 17:40 
GeneralRe: There are many gotos, but these ones are mine Pin
honey the codewitch11-May-24 18:01
mvahoney the codewitch11-May-24 18:01 
GeneralRe: There are many gotos, but these ones are mine Pin
trønderen12-May-24 8:15
trønderen12-May-24 8:15 
GeneralRe: There are many gotos, but these ones are mine Pin
honey the codewitch12-May-24 8:28
mvahoney the codewitch12-May-24 8:28 
GeneralRe: There are many gotos, but these ones are mine Pin
honey the codewitch12-May-24 13:56
mvahoney the codewitch12-May-24 13:56 
GeneralRe: There are many gotos, but these ones are mine Pin
den2k8812-May-24 23:08
professionalden2k8812-May-24 23:08 
GeneralRe: There are many gotos, but these ones are mine Pin
trønderen13-May-24 0:17
trønderen13-May-24 0:17 
GeneralRe: There are many gotos, but these ones are mine Pin
giulicard13-May-24 6:59
giulicard13-May-24 6:59 
trønderen wrote:
If I were given the responsibility for a state machine implementation like that, I would immediately run to my boss asking for permission to rewrite the whole thing as a table driven machine.


... or as a state machine that returns function pointers instead of using tables and state variables:

C++
#include <stdio.h>
#include <conio.h>

// Fn ptrs defs
typedef  void (*RT)( int input );
typedef  RT (*TER)( int input );

// Forward declarations
extern TER state1( int input );
extern TER state2( int input );
extern TER state3( int input );

// First state
TER state1( int input )
{
    printf( "one\t" );
    return input < 10 ? (TER)&state2 : (TER)NULL;
}

// Second state
TER state2( int input )
{
	printf( "two\t" );
	return (TER)&state3;
}

// Third state
TER state3( int input )
{
    printf( "three\t" );
    return (TER)&state1;
}

int main(int argc, char* argv[])
{
    int n;

    // Set Start state
    TER state = (TER)&state1;

    // Exercises the state machine. Ends when state == NULL
    for ( n = 0 ; state ; ++n ) {
        // Executes the current state (state variable) then goes to the next state
        state = (TER)( state( n ) );
    }

    printf( "\n\nPress any key\n" );
    getch();

	return 0;
}


Type casts are useful because in C it's impossible to declare function pointers that return function pointers that return function pointers that return function pointers... Smile | :)

Regards
GeneralRe: There are many gotos, but these ones are mine Pin
honey the codewitch13-May-24 9:21
mvahoney the codewitch13-May-24 9:21 
GeneralRe: There are many gotos, but these ones are mine Pin
giulicard13-May-24 10:03
giulicard13-May-24 10:03 
GeneralRe: There are many gotos, but these ones are mine Pin
honey the codewitch13-May-24 10:04
mvahoney the codewitch13-May-24 10:04 
GeneralRe: There are many gotos, but these ones are mine Pin
giulicard13-May-24 10:07
giulicard13-May-24 10:07 
GeneralRe: There are many gotos, but these ones are mine Pin
trønderen14-May-24 5:10
trønderen14-May-24 5:10 
GeneralRe: There are many gotos, but these ones are mine Pin
honey the codewitch14-May-24 6:21
mvahoney the codewitch14-May-24 6:21 
GeneralRe: There are many gotos, but these ones are mine Pin
trønderen14-May-24 13:12
trønderen14-May-24 13:12 
GeneralRe: There are many gotos, but these ones are mine Pin
honey the codewitch14-May-24 13:14
mvahoney the codewitch14-May-24 13:14 
GeneralRe: There are many gotos, but these ones are mine Pin
trønderen14-May-24 14:12
trønderen14-May-24 14:12 
GeneralRe: There are many gotos, but these ones are mine Pin
honey the codewitch14-May-24 14:13
mvahoney the codewitch14-May-24 14:13 
GeneralRe: There are many gotos, but these ones are mine Pin
trønderen14-May-24 14:25
trønderen14-May-24 14:25 
GeneralRe: There are many gotos, but these ones are mine Pin
honey the codewitch14-May-24 14:31
mvahoney the codewitch14-May-24 14:31 
GeneralRe: There are many gotos, but these ones are mine Pin
trønderen14-May-24 15:02
trønderen14-May-24 15:02 
GeneralRe: There are many gotos, but these ones are mine Pin
honey the codewitch14-May-24 15:03
mvahoney the codewitch14-May-24 15:03 
GeneralRe: There are many gotos, but these ones are mine Pin
trønderen14-May-24 17:41
trønderen14-May-24 17:41 
GeneralRe: There are many gotos, but these ones are mine Pin
honey the codewitch14-May-24 18:06
mvahoney the codewitch14-May-24 18:06 
GeneralRe: There are many gotos, but these ones are mine Pin
honey the codewitch14-May-24 18:09
mvahoney the codewitch14-May-24 18:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.