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

  • SEARCH
  • Home
  • 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 6981111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:06:51+00:00 2026-05-27T18:06:51+00:00

Below code produces two smiley faces when I call case 2 after case 1

  • 0

Below code produces two smiley faces when I call case 2 after case 1 (in other words after one while loop). However printSentence(); works as it should be in case 1.

#include <stdio.h>
#include <string.h>

    char *enterSentence();
    void printSentence(char *);
    char *sentence;
    int willContinue = 1;

main() {
    while (willContinue) {
    int a;
    scanf("%d", &a);
    switch (a) {
           case 1:
                getchar();
                sentence = enterSentence();
                printSentence(sentence);
                break;
           case 2:
                getchar();
                printSentence(sentence);
                break;
           case 3:
                willContinue = 0; //exit
                break;
                }
    }
}

 char *enterSentence() {
         char temp[999];
         gets(temp);
         return temp;
      }

 void printSentence(char *asd) {
         puts(asd);
      }
 .
 . //more code
 .

I wonder what is the problem here, thanks for any help..

  • 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-27T18:06:52+00:00Added an answer on May 27, 2026 at 6:06 pm

    temp is local to the function enterSentence. It is created when the function is entered and it is destroyed when the function terminates.

    When you return the address of the object (return temp;) it still exists and has that address, but it will be immediately destroyed afterwards and the calling function receives a pointer to an invalid location.

    Quick and dirty solution: make temp a static object that can live since the program started till it ends

    static char temp[999];
    

    Note: static is a quick and dirty solution, as I said. It is mostly better avoided.


    Edit

    Slow and clean solution: move the temp object to the calling function and pass its pointer to the function

    int main(void) {
        char temp[999];
        /* ... */
        enterSentence(temp, sizeof temp);
        /* ... */
    }
    
    size_t enterSentence(char *dst, size_t len) {
        size_t retlen;
        fgets(dst, len, stdin);
        retlen = strlen(dst);
        if (dst[retlen - 1] == '\n') dst[--retlen] = 0;
        return retlen;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below produces this output fine: This is page one. This is page
I have two code examples below. One works, but the second (using extend to
The below code produces output consisting of two green items. Between those items there
Having the code below in my Global.asax.cs and two controller (one based on a
The code below produces a NumberFormatException in this line: val cache = cf.createCache(Collections.emptyMap()) Do
Given the code below, how would you create/implement SR.h so that it produces the
The below code seems like it should work; however, the blob in the database
How to combine two data columns into one file. These code should produce a
Heres some code for a test case. I don't understand why the first two
Despite the fact that my JDO query contains TWO declareParameters statements, the code below

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.