Help in memcmp (it may be a bug)


 
Thread Tools Search this Thread
Top Forums Programming Help in memcmp (it may be a bug)
# 1  
Old 03-21-2012
Help in memcmp (it may be a bug)

Hello friends today i have created a program that is working fine . but when string becomes equal it does not work .

Code:
/* String.h header series
	
	Comparison function

memcmp = return 0 if string equal  else return less or greater than value by comparing pointer first and pointer second

*/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
/*memcmp operation*/
int i;
char str[20];
char str2[20];
printf("Enter first string: ");
fgets(str,20,stdin);
printf("Enter second string: ");
fgets(str2,20,stdin);
fflush(stdin);
i = memcmp(str,str2,20);
if(i>0){
printf("%s greater than %s\n",str,str2);
}
else if(i<0)	{
	printf("%s greater than %s\n",str2,str);
}
else {   printf("Both are eqaul\n");  }
return 0;  
}

output:
Code:
[root@apache ~]# ./mem
Enter first string: hello
Enter second string: hello
hello greater than hello

memcmp is not working if string equal and it does not return 0 on equality of string , is this a bug ?

any help
# 2  
Old 03-21-2012
for the same input "hello"

can you try with the below code

Code:
 
i = memcmp(str,str2,5);

# 3  
Old 03-21-2012
Consider using strcmp
# 4  
Old 03-21-2012
hello is 5 characters long. Not 20.

strcmp, strncmp and other similar functions are used to do string compares. A C string is terminated with a nul (ascii 0) character. This allows strcmp to determine the string's length and stop comparing. memcmp compares objects in memory. It does not check the length because it is meant to compare memory objects which often have ascii 0 characters.

You provide the length to memcmp. 20 is not the length of the word hello
# 5  
Old 03-21-2012
To the program work the way you expect, you need to initialize the str and str2 memory areas with the same values (say, with all zeroes, for example):

PHP Code:
(...)
char str[20];
char str2[20];

memset(str0sizeof(str)); 
memset(str20sizeof(str2));
(...) 
If you donīt initialize str and str2 with the same values, these areas will contain random values, so the memcmp comparison will not match, even if they both have some values in common (both begin with 'h','e','l','l','o', but you donīt know what each have after the 5 first characters, cause it is uninitialized memory).
This User Gave Thanks to pflynn For This Post:
# 6  
Old 03-21-2012
Thanks to pflynn now it is working

i have used memset as u say it great idea first get string and fill it with 0 then compare it .


thanks pflynn
# 7  
Old 03-21-2012
Yes Rink, but be aware that for string comparisons, strcmp - and its safe version, strncmp - are better suited, since they are specifically designed for working with strings.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

A $(( expression )) bug?

This is for the big guns... I have been modifying AudioScope.sh to bring it inline with more current practices. I hit a bug which IS not present in the original code but was after modification. Shell check first:- #!/bin/sh txt="1234567890" echo "$(( $txt ))" echo "$(( ${#txt} - 1 ))" echo... (17 Replies)
Discussion started by: wisecracker
17 Replies

2. Shell Programming and Scripting

How to fix this bug?

Hi gurus, I have script as below: #!/bin/ksh while : do cat /dev/null > srcfile exit_time=`TZ=GMT-20 date +%Y%m%d1950` cur_time=`date +%Y%m%d%H%M` flag=1 if ; then ... (7 Replies)
Discussion started by: ken6503
7 Replies

3. Programming

is this a bug of g++?

Hello, Im using the g++(g++ Ubuntu/Linaro 4.4.4-14ubuntu5 4.4.5) and im trying to compile a small snippet code and got into an endless loop.I recompiled that in VS2010 under Windows 7 and the answer is as expected.so i wonder is this a bug of g++?here is my code. #include<iostream> using... (5 Replies)
Discussion started by: homeboy
5 Replies

4. UNIX for Dummies Questions & Answers

where's the bug?

#!/bin/bash if then #echo "infinite loop" exit 0 fi when I run this file I get the following error: ./test_infinite_loop: line 5: syntax error near unexpected token `fi' ./test_infinite_loop: line 5: `fi' :confused: (4 Replies)
Discussion started by: jon80
4 Replies

5. AIX

bug in 43 ???

xxxxserver# lsattr -El inet0 | grep 255.240.0.0,32.224.0.0,32.78.120.254 | grep '.40' route net,-hopcount,1,-netmask,255.240.0.0,32.224.0.0,32.78.120.254 How this is possible? (1 Reply)
Discussion started by: itik
1 Replies

6. Shell Programming and Scripting

Is it a bug ..?

Hi All, I am using Red Hat Linux on my servers. The problem that I am facing is, sometimes the /opt usage on the server shows used percentage as 100% , when actually it is simply 20%. When I reboot the system, it comes back to 20%.Is this a bug in the system or my settings have gone wrong... (1 Reply)
Discussion started by: nua7
1 Replies

7. Post Here to Contact Site Administrators and Moderators

Have I found a bug?

When searching for new posts, I see that my voting in one of the polls counts as a 'new post'. However, while the '<blah> minutes ago' entry updates correctly, the 'by <username>' is the last user to actually post a comment in the poll instead. Result: Poll: vB Guest Book 39... (4 Replies)
Discussion started by: Smiling Dragon
4 Replies

8. Programming

Bug has developed

Guys there is another bug that has developed.........now messaged above 6 bytes are trimmed and echoed back to client.After the first message the echo is not regular.I have put a loop in both client and server to check for closing.Moreover for connection close i also need to put another clause that... (3 Replies)
Discussion started by: arjunjag
3 Replies
Login or Register to Ask a Question