Thread: Scanf/gets string act as sort of a stack .

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    11

    Scanf/gets string act as sort of a stack .

    Hello everyone ,

    I have a function in my code which gets a string , sends it to another function for validation , and if the string
    turns out to be invalid , then it calls the function again (which gets the string) .

    I have made a simple code which demonstrates my question :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        char string[30];
        short i;
        scanf("%s",string);
        if(strcmp(string,"1")==0)
            main();
        printf("%s",string);
    }
    Let us say that a valid string is all the strings which are not "1" .
    If one enters three times "1" ("1", calls main() , "1" ,etc..) and then enters "2" , the output would be : "2111" .

    My first thought was that the string wont re-initialize , so I have added a bit to my code which would put '\0' (end of string)
    in each char :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        char string[30];
        short i;
        for(i=0;i<30;i++)
            string[i]='\0';
        scanf("%s",string);
        if(strcmp(string,"1")==0)
            main();
        printf("%s",string);
    }
    As you can see - right after the declaration of the variable , I loop through each char and put '\0' in it ,
    but the output stays the same ("2111") .
    I have tried to replace the input/output functions , replace the manual loop to strcpy(string,"") , the output stays the same .

    I`d really like to know what am I missing here , why wont the output just be "2" ?

    Thank you much ,

    Danny .

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    scanf("%s",&string)

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    11
    ^ A string is already a pointer to the first char , there is no need to specify an ampersand there (also I tried it and the output is the same) .

    Thank you for the quick answer though ,

    Danny .

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The function main IS NOT SUPPOSED to be used an an recursive function.

    recursive main
    http://stackoverflow.com/questions/4...519407#4519407

    Tim S.
    Last edited by stahta01; 06-14-2011 at 08:18 AM.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    11
    @stahta01 Thanks! In my code (this is just an example , it is not my code which I`m talking about) the recursive function is not main() .
    I have updated the code to the following :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void testOutput()
    {
        char string[30];
        short i;
        for(i=0;i<30;i++)
            string[i]='\0';
        scanf("%s",string);
        if(strcmp(string,"1")==0)
            testOutput();
        printf("%s",string);
        return;
    }
    int main()
    {
        testOutput();
        return 0;
    }
    And the output stays the same .

    Thanks again ,

    Danny .
    Last edited by Dann Shem; 06-14-2011 at 08:20 AM.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Dann Shem View Post
    @stahta01 Thanks! In my code (this is just an example , it is not my code which I`m talking about) the recursive function is not main() .
    I have updated the code to the following :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void testOutput()
    {
        char string[30];
        short i;
        for(i=0;i<30;i++)
            string[i]='\0';
        scanf("%s",string);
        if(strcmp(string,"1")==0)
            main();
        printf("%s",string);
        return;
    }
    int main()
    {
        testOutput();
        return 0;
    }
    And the output stays the same .

    Thanks again ,

    Danny .
    You are STILL calling main!!!!

    Tim S.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    11
    True , sorry , edited .
    Output is still the same .

    Thanks ,

    Danny .

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest trying the below code; it might work.
    Code:
    if(strcmp(string,"1")==0){
            testOutput();
    } else {
        printf("%s",string);
    }

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    100
    You are creating new storage for string every time you enter your function.
    Maybe you want to pass it? Or make it static?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > I`d really like to know what am I missing here , why wont the output just be "2" ?
    I'm confused - if you only want to see 2, then stop printing the bad input.

    Perhaps consider this logic, as opposed to indefinite recursion (which has it's own issues).
    Code:
    while ( fgets( string, sizeof string, stdin ) != NULL ) {
      if ( strncmp( string, "1", 1 ) == 0 ) {
        // success
        break;
      } else {
        // an error message perhaps
      }
    }
    Which will loop until the user signals EOF, or gets the input right.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    11
    My dear friends , thank you for everything , but this is not my actual code - this is an example which results the same as my code .
    My actual code is validating an input for a name a user enters which will follow to enter to a linked list .
    I sure can avoid this , but the question is just out of curiosity , the code is already fixed .

    Thanks again to all of you ,

    Danny .

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    11
    Any luck ?

    Thanks , Danny .

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > I sure can avoid this , but the question is just out of curiosity , the code is already fixed .
    You said it was fixed, so we moved on.
    If you have another question, start a new thread.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    11
    The code has been fixed indeed , but as I said - I`m still very curious for why the output is what it is .
    What does a fixed code give me if I do not understand the root of the problem with the previous one ?

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Maybe you can get us up to date by (re)posting the code and your specific question(s)?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Sort - stack overflow
    By ButterCup in forum C Programming
    Replies: 7
    Last Post: 03-22-2010, 08:31 AM
  2. Scanf String
    By Demipimp in forum C Programming
    Replies: 9
    Last Post: 12-05-2008, 11:12 AM
  3. recursive quick sort - stack overflow
    By Micko in forum C Programming
    Replies: 9
    Last Post: 01-01-2005, 05:51 PM
  4. Search and Sort functions for a stack..
    By blizeH in forum C++ Programming
    Replies: 7
    Last Post: 01-24-2003, 01:36 PM
  5. Stack Sort
    By fluf in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2002, 07:44 PM