state machine using function pointers

This is a discussion on state machine using function pointers within the C Programming forums, part of the General Programming Boards category; Hi, I would like to use function pointers to code a state machine. That is if I have the following ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    1

    state machine using function pointers

    Hi,
    I would like to use function pointers to code a state machine.
    That is if I have the following events possible in 2 states S1, S2

    S1: E1, E2
    S2: E3, E4

    and I have processing functions for all possible events for all states, and corresponding function pointers

    fpS1E1, fpS1E2, fpS2E3, fpS2E4, corresponding to the processing of a particular event in a particular state.


    Now if I define a function pointer two dimensional array like
    Code:
    void  *fpArray[][]() =  { {fpS1E1,fpS1E2}, {fpS2E3,fpS2E4}};
    
    and if 
    #define S1 0
    #define S2 1
    #Define E1 0
    #define E2 1,
    
    state = S1;
    while (1)
    {
         eventid = receive_event();
         fpArray[state][eventid]; 
    }
    
    where typically inside function 
    fpS1E1() { ...
                      state = S2 (any new state)
                   }
    the state machine will function right? Or is there any problem in my logic/code?
    I have omitted function pointer initialisations( fpS1E1 = s1e1function and the like)

    Meena
    Last edited by meena_selvam; 10-25-2008 at 02:07 PM.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    No. You aren't actually calling the functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21