Thread: goto or function pointer into this pseudo-code?

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Italy
    Posts
    53

    goto or function pointer into this pseudo-code?

    I have a doubt if to use `goto` or `pointer to a function` into my code. Here a pseuco C code using goto:


    Code:
    /* code that read the answer */
    switch-case:
    switch(answer)
    case LIST:
    goto list_files;
    case PWD:
    goto pwd;
    case RETR:
    goto retr_file;
    where:


    Code:
    list_files:
    /* all code needed to list files */
    goto switch-case;
    pwd:
    /* all code needed to print the current working directory */
    goto switch-case;
    retr-file:
    /* all code needed to retrive a file */
    goto switch-case;
    Regard the pseudo code with function pointer i have no idea on how to it xD
    Can this be a good code? Because i think that using function pointer will complicate my program too much!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Let's say NO to the goto, OK?

    If you need to break out of a deep nested set of loops, then a - ONE - goto is fine. But this repeating goto's for your logic, is UGLY.

    If you have a switch case that needs to be called multiple times, from different places in your code, make it into it's own small function, and call it like any other function. If you feel the need to ask about using a function pointer, then imo, you're being just 10% too clever for your own good.

    So I say NO to the repetitive goto's - use good solid logic instead, and no to the function pointer, for now. There's nothing magical about a function pointer. If it's confusing you right now, don't use it just yet.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should put "list_files", "pwd", and "retr_file" into their own functions. Then you can have nice clean code that works as described in your pseudo code, while avoiding complications like "goto" and function pointers altogether.

    Code:
    while(true)
    {
        switch(answer)
        {
            case 'x':
                list_files();
                break;
            case 'y':
                 pwd();
                 break;
            case 'z':
                 retr_file();
                 break;
            default:
                 break;
        }
    }

  4. #4
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Like Adak said, split each of the cases into a separate function instead. Edit: Exactly as Matticus showed.

    For example, assume you use this function to calculate a perfect hash of the four character FTP command at the start of the string, save the hash to an int, and return a pointer to the possible arguments following the FTP command. The function will return NULL if there is anything suspicious.
    Code:
    static inline const char *parse_command(const char *input, int *const cmdptr)
    {
        int cmd = 0;
    
        /* Nothing to parse? */
        if (!input || !*input)
            return NULL;
    
        /* Skip leading control and whitespace characters. */
        while (*input > 0 && *input <= 32)
            input++;
    
        /* Parse the four-character command to cmd.
         * input is parsed as a base-40 number, with
         * A = 1, .. Z = 26, a = 1, .., z = 26, zero = 28, .., nine = 37.
         *     AAAA = 65361
         *     AAAB = 65362
         *     ZZZZ = 170666
         *     0000 = 1837948
         *     9999 = 2428717
         * While technically the range is 0 .. 2559999,
         * only 65361..2428717 are produced by this function.
         * (not even all of them, there are holes due to 0, 27, 38, 39 not used)
        */
        while (cmd < 64000)
            if (*input >= 'A' && *input <= 'Z')
                cmd = 40 * cmd + (int)(*(input++) - 'A' + 1);
            else
            if (*input >= 'a' && *input <= 'z')
                cmd = 40 * cmd + (int)(*(input++) - 'a' + 1);
            else
            if (*input >= '0' && *input <= '9')
                cmd = 40 * cmd + (int)(*(input++) - '0' + 28);
            else
                return NULL;
    
        /* Skip trailing spaces. */
        while (*input == ' ')
            input++;
    
        /* Save the command number. */
        if (cmdptr)
            *cmdptr = cmd;
    
        return input;
    }
    Your command parser function can then utilize the above function to find out the command number, then call the suitable worker function:
    Code:
    int handle_ftp_command(const char *const input)
    {
        const char *args; /* Start of arguments in input */
        int     cmd = -1; /* The command number */
    
        args = parse_command(input, &cmd);
        if (!args) {
            /* The input line is not a valid command. */
            return EINVAL; /* "invalid input" return code. */
        }
    
        switch (cmd) {
    
        case 1374618: /* USER */
            return ftp_USER(args);
    
        case 1026379: /* PASS */
            return ftp_PASS(args);
    
        case 783180: /* LIST */
            return ftp_LIST(args);
    
        case 1121980: /* QUIT */
            return ftp_QUIT(args);
    
        default:
            /* Command cmd is not implemented. */
            return ENOSYS;
        }
    }
    The ftp_USER(), ftp_PASS(), ftp_LIST(), and ftp_QUIT() functions all take the string following the command as an argument, and return success (0) or failure (I used errno codes above), just like the handle_ftp_command() function itself.

    It is customary to use zero as success and nonzero as error, since you can only have one success type, but multiple error types. (Or you could use negative values for errors, zero for success, and positive values for success with some sort of caveat or comment.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pseudo Code (Please Help)
    By slimdime in forum C++ Programming
    Replies: 1
    Last Post: 09-05-2010, 09:47 AM
  2. pseudo-code
    By Seb in forum C Programming
    Replies: 7
    Last Post: 04-27-2009, 01:36 PM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  4. what is pseudo-code
    By The_Master in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2006, 06:53 AM
  5. Recursive Function in Pseudo Code
    By smitsky in forum Tech Board
    Replies: 3
    Last Post: 10-24-2004, 10:17 AM

Tags for this Thread