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: 381
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
    594
    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,572
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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,572
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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