Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9130393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:57:28+00:00 2026-06-17T07:57:28+00:00

I’m trying to build a serial command interpreter, so I want to store my

  • 0

I’m trying to build a serial command interpreter, so I want to store my commands in an array. I want each command to have a name and a function pointer so that I can compare the command name to what I typed into and then call the function. I’m not that good with C, so please help! Here is what I have so far.

The command array will be an array of structs. Each struct will have a string and a function pointer. There are errors here, but I don’t know how to fix them. These are done before main.

typedef struct cmdStruct {
    char cmd[16];
    void (*cmdFuncPtr)(void);
}CmdStruct;

void (*ledFuncPtr)(void);
void (*cmd2FuncPtr)(void);

// assign pointers to functions
ledFuncPtr = &LedFunction;
cmd2FuncPtr = &Cmd2Function;

//build array of structs
CmdStruct cmdStructArray[] = cmdStructArray = { {"led",   ledFuncPtr   },
                                                {"cmd2",  cmd2FuncPtr  },  };

Later on, I will go through the struct array to compare it to the received command.

// go through the struct array to do string comparison on each struct's string member
for (int i = 0; i < sizeof(cmdStructArray); i++) {
    // string comparison of received command and string of struct
    if(strcmp(cmdStructArray[i].cmd, receivedCmd)==0) {
        // dereference function pointer
        (*cmdStructArray[i].cmdFuncPtr)(void);
    }
}

What parts am I doing wrong, and how do I fix them?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:57:29+00:00Added an answer on June 17, 2026 at 7:57 am

    As it has already been noted, your cycle makes wrong number of iterations. sizeof array does give you the number of elements in the array, but rather the number of bytes in the array. You have to calculate sizeof array / sizeof *array to get the number of elements.

    Also, your function call syntax is invalid

     (*cmdStructArray[i].cmdFuncPtr)(void);
    

    The above will not compile. You cannot specify void as an argument in function call. (void) syntax can only be used in function declarations. If the function accepts no parameters, the call should look as

     (*cmdStructArray[i].cmdFuncPtr)();
    

    Also, this will not compile as well

    CmdStruct cmdStructArray[] = cmdStructArray = { {"led",   ledFuncPtr   },
                                                    {"cmd2",  cmd2FuncPtr  },  };
    

    Why are you mentioning cmdStructArray in this declaration twice?


    Some additional, essentially cosmetic remarks:

    Firstly, since your commands are probably going to be string literals known at compile time, you can declare the first member of your struct as a const char * pointer instead of char array

    typedef struct cmdStruct {
      const char *cmd;
      void (*cmdFuncPtr)(void);
    } CmdStruct;
    

    The initialization syntax does not change. This will relieve you of the need to worry about the size of the array (16 that you currently have there).

    Secondly, it is not clear why you had to declare intermediate pointers to functions ledFuncPtr and cmd2FuncPtr instead of initializing your array directly. What was the purpose of this

    void (*ledFuncPtr)(void);
    void (*cmd2FuncPtr)(void);
    
    // assign pointers to functions
    ledFuncPtr = &LedFunction;
    cmd2FuncPtr = &Cmd2Function;
    
    CmdStruct cmdStructArray[] = { {"led",   ledFuncPtr  },
                                   {"cmd2",  cmd2FuncPtr }, };
    

    when you could simply do this

    CmdStruct cmdStructArray[] = { {"led",   &LedFunction  },
                                   {"cmd2",  &Cmd2Function }, };
    

    (without introducing ledFuncPtr and cmd2FuncPtr at all)?

    Thirdly, you don’t have to use * and & operators with function pointers. This will work too

    CmdStruct cmdStructArray[] = { {"led",   LedFunction   },
                                   {"cmd2",  Cmd2Function  }, };
    

    and

    cmdStructArray[i].cmdFuncPtr();
    

    Anyway, this is a purely cosmetic issue, a matter of personal preference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into
I have an array which has BIG numbers and small numbers in it. I
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I want to construct a data frame in an Rcpp function, but when I
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.