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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:38:11+00:00 2026-05-20T09:38:11+00:00

Thanks! Works perfectly now. Java has made me stupid :( I am having some

  • 0

Thanks! Works perfectly now. Java has made me stupid 🙁

I am having some difficulty comparing strings in C. I get correct output when I don’t use my isMorse function, but when I use it the output becomes inaccurate and displays random characters. As far as I can tell, the variable “morse” is actually changed when strcmp is called on it. I am thinking that it has to do with “morse” not being a constant, but I am unsure of how to remedy it.

Thanks!!

char *EnglishToMorse(char english)
{
   static char *morse;

   int i;
   for (i = 0; i < LOOKUP_SIZE; i++)
   {
      if (lookup[i].character == english)
      {
         morse = lookup[i].morse;
         return morse;
      }
   }

   morse = &english;  // Problem was here!!!
   return morse;
}
  • 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-20T09:38:12+00:00Added an answer on May 20, 2026 at 9:38 am

    The reason your morse variable appears to change is because it points to an area on the stack. The reason it points to an area on the stack is because you assigned it the address of your parameter english, which got pushed onto the stack when you called your function then popped off the stack once the function completed.

    Now your morse variable will point to whatever memory takes that same location on the stack, which will constantly change throughout the lifetime of your program.

    In my opinion, the best way to fix this problem would be to return a NULL pointer from EnglishToMorse if the character is not A-Z… then check for the NULL pointer in your isMorse function. After all, it’s good practice to check for NULL pointers in code.

    char* EnglishToMorse(char english)
    {
        int i;
    
        english = toupper(english);
        for (i = 0; i < LOOKUP_SIZE; i++)
        {
            if (lookup[i].character == english)
                return lookup[i].morse;
        }
    
        return NULL;
    }
    
    int isMorse(char* morse)
    {
        int i;
    
        /* Check for NULL, so strcmp doesn't fail. */
        if (morse == NULL) return 0;
    
        for (i = 0; i < LOOKUP_SIZE; i++)
        {
            if(strcmp(morse, lookup[i].morse) == 0) 
                return 1;
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My jQuery right now works perfectly when I use it to perform creation, and
I am having difficulty executing a MS SQL Server stored procedure from Java/jsp. I
I have created an iPhone app which works perfectly smooth on my simulator. Now
Having work a bit and thanks to the answer I am now using :
Thanks for reading this. I am dynamically generating some data which includes a select
Thanks for a solution in C , now I would like to achieve this
Thanks to a library upgrade (easymock 2.2 -> 2.4), we're having tests that have
I have created a Java webapplication containing a tobago sheet. I am now trying
Dear all,Now i have this question in my java program,I think it should be
I have an app that works perfectly on my local machine and am deploying

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.