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 6728131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:07:57+00:00 2026-05-26T10:07:57+00:00

I have the following if-statements: if (strcmp(registerName, zero)) return 00000; else if (strcmp(registerName, at))

  • 0

I have the following if-statements:

    if (strcmp(registerName, "zero"))
            return 00000;
    else if (strcmp(registerName, "at"))
        return 00001;
    else if (strcmp(registerName, "v0"))
        return 00010;
    else if (strcmp(registerName, "v1"))
        return 00011;

It’s actually really long – about 20 if-statements. Therefore, I would like to use a switch. How would I convert that to switch when each statement has a different condition?

I tried something as the code below, but it does not work:

int registerAddress;

switch(registerAddress) {

case 1 (strcmp(registerName, "zero")):
        regsiterAddress = 00000;
        break;
case 2 (strcmp(registerName, "at")):
        registerAddress = 00001;
        break;
}
  • 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-05-26T10:07:58+00:00Added an answer on May 26, 2026 at 10:07 am

    You can’t — switch statements in C only work on primitive types, not on strings. You could use, say, a hash table or a search tree to optimize the matching, but for only 20 options that may not be worth the trouble.

    What you could do, to clean up the code, is set up a mapping table:

    struct str2Num {
        char *str;
        int num;
    };
    
    const struct str2Num registerMap[] = {
        { "zero", 00000 },
        { "at", 00001 },
        { "v0", 00010 },
        { "v1", 00011 },
        { NULL, 0 }  /* end marker */
    };
    

    and do your matching like this:

    int i;
    for (i = 0; registerMap[i].str != NULL; i++) {
        if (strcmp(registerName, registerMap[i].str) == 0) {
            return registerMap[i].num;
        }
    }
    /* handle no-match case here */
    

    In fact, if you sorted the table alphabetically, you could even use bsearch() for fast matching.

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

Sidebar

Related Questions

FIRST PROBLEM I have timed how long it takes to compute the following statements
I have used the following statements to get the current time. print $query executed
simple question :- i have the following simple if (..) statements :- if (foo
I have a little confusion in the following two statements. The below program is
I have the following if statements, two of which don't seem to work. I
Does anybody have any suggestions on how to improve the following statements: insert into
I have following statements in Oracle query window in VS2010, It does not show
Do I have any way to simplify the following statements? (probably, using boost::enable_if )
So I have the following SELECT statements: SELECT COUNT(A.Award) AS US, SUBSTRING(CAST(M.Year as char(4)),
How does PHP read if statements? I have the following if statements in this

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.