This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

c program

hello;
i want to know that where we use link list, function pointer, generic pointer in embedded programming

Parents Reply Children
  • I think Christoph means to avoid directly nesting switch statements, particularly with a lot of inline code in the cases:

       switch (a)
          {
          case a1 :
             switch (b)
                {
                case b1 :
                   statement b1;
                   switch (c)
                      {
                      ...
                      }
                    break;
    ...
    

    If each case just calls a function, those functions might happen to have switch statement inside, but won't cause readability and indentation problems. In fact, if every case in the switch calls a function, it's quite likely that it will compile into a jump table to those function calls, which is essentially the same code as a table of function pointers.

  • My faviourite (not!) is state machines where the states has not been given names. Each case just has a number, and within it the code may have a line:

    state = 17;
    

    Almost impossible to know what state 17 means, and almost impossible to insert a new state without missing to renumber all state changes.

  • My faviourite (not!) is state machines where the states has not been given names.

    Ohhh, I think you forgot that for complete "enjoyment", all of the states are actually "documented" in a separate document, which is "slightly" out of date with the actual code. :)

    (It's somewhere in the Writing-Unmaintainable-Code Howto: "Lie in the comments/documentation. It doesn't need to be obvious, just fail to keep the comments/documentation up to date with the code.")

  • If each case just calls a function, those functions might happen to have switch statement inside, but won't cause readability and indentation problems.

    Exactly, thanks for guessing my thoughts.

    #define STATE_INIT 0
    #define STATE_WORK 1
    
    void state_machine()
    {
       static int current_state = STATE_INIT;
    
       switch(current_state)
       {
          case STATE_INIT:
             current_state = state_init();
             break;
          case STATE_WORK:
             current_state = state_work();
             break;
          default:
             current_state = STATE_INIT;
             break;
       }
    }
    
    int state_init()
    {
       return(STATE_WORK);
    }
    
    int state_work()
    {
       return(STATE_WORK);
    }
    
    

    Something along those lines.