Like Tree1Likes
  • 1 Post By stahta01

Simple strcmp program freezes upon scanf

This is a discussion on Simple strcmp program freezes upon scanf within the C Programming forums, part of the General Programming Boards category; Hello. This is a simple program that imports a custom header file called 'myfunc.h,' which contains a defined prototype for ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    6

    Simple strcmp program freezes upon scanf

    Hello.

    This is a simple program that imports a custom header file called 'myfunc.h,' which contains a defined prototype for an alternative strcmp function.

    The code for the .c file is:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "myfunc.h"
    
    
    int main(void) {
    	char* string1 = {0};
    	char* string2 = {0};
    	
    	printf("Enter string1: ");
    	scanf("%s", string1);
    	printf("Enter string2: ");
    	scanf("%s", string2);
    	
    	if (compstring(string1, string2) == 1) { printf("The two strings you entered are the same.\n"); }
    	else { printf("The two strings you entered are different.\n"); }
    
    
    	system("pause");
    
    
    	return 0;
    }
    and this is the header file:

    Code:
    #include <string.h>
    
    
    int compstring(char* str1, char* str2) {
    	if (strcmp(str1, str2) == 0) { return 1; }
    	else { return 0; }
    }
    Here is the resulting error:
    Name:  thXcW.png
Views: 546
Size:  67.6 KB


    Any ideas on what could be causing this? Thanks in advance.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    652
    try making the string a static array first it could make a difference, you have a string literal which can not be modified if im not mistaken

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,926
    I suggest making string1 and string2 into arrays instead of a pointers.
    Or, allocate space for each pointer to hold the data.

    Tim S.
    Salem likes this.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    Quote Originally Posted by stahta01 View Post
    I suggest making string1 and string2 into arrays instead of a pointers.
    Or, allocate space for each pointer to hold the data.

    Tim S.
    Thank you for your help.

    I changed:
    Code:
    char* string1 = {0};
    char* string2 = {0};
    to:
    Code:
    char string1[] = {0};
    char string2[] = {0};
    and now the function returns 0 each time, even when the strings entered are the same, although I am not getting any errors during compile.

    To reiterate, the custom header was:
    Code:
    #include <string.h>
    
    
    int compstring(char* str1, char* str2) {
        if (strcmp(str1, str2) == 0) { return 1; }
        else { return 0; }
    }
    Why would the passed strings not return a value that indicates they are equivalent?

    Thank you again.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,926
    You need to allocate space to hold the information.
    You are allocating a single char per array; this is only enough to hold the empty string.

    Try
    Code:
    char string1[255] = {0};
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,629
    Quote Originally Posted by drbr0wn
    Why would the passed strings not return a value that indicates they are equivalent?
    I tried this program:
    Code:
    #include <string.h>
    #include <stdio.h> 
     
    int compstring(char* str1, char* str2) {
        if (strcmp(str1, str2) == 0) { return 1; }
        else { return 0; }
    }
    
    int main(void) {
        char string1[] = {0};
        char string2[] = {0};
        printf("%d\n", compstring(string1, string2));
        return 0;
    }
    and it printed 1.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    Quote Originally Posted by stahta01 View Post
    You need to allocate space to hold the information.
    You are allocating a single char per array; this is only enough to hold the empty string.

    Try
    Code:
    char string1[255] = {0};
    Tim S.
    Aha! Thank you!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-02-2011, 09:17 PM
  2. Simple strcmp parameter problem.....
    By INFERNO2K in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 11:34 AM
  3. Simple help (STRCMP, word counts etc)
    By mariojoshi in forum C Programming
    Replies: 22
    Last Post: 12-11-2004, 04:33 AM
  4. program freezes - structure searching.
    By Vber in forum C Programming
    Replies: 9
    Last Post: 03-20-2003, 08:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21