473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

difference between memcmp and strcmp....

which of the 2 commands are applicable in comparing an array of unsigned chars?

if(strcmp(aAbsCylNumHigh, bAbsCylNumHigh)<=0 && strcmp(aAbsCylNumLow,bAbsCylNumLow)<=0 && strcmp(aSecNum,.bSecNum)<0 )

or

if(memcmp(aAbsCylNumHigh, bAbsCylNumHigh,2)<=0 && memcmp(aAbsCylNumLow,bAbsCylNumLow,2)<=0 && memcmp(aSecNum,.bSecNum,2)<0 )
Oct 9 '06 #1
4 14235
Banfa
9,065 Expert Mod 8TB
The difference is in the third parameter (not there in strcmp).

Basically strcmp assumes you have passed pointers to zero terminated strings and so the compare stops when it finds the first terminator '\0' in either string.

memcmp makes no such assumption so you have to pass it the number of bytes to compare.

In English

strcmp compares 2 arrays of char that contain zero terminated strings

memcmp compares 2 arrays of char that contain any data.
Oct 9 '06 #2
ok i see...
what can you suggest for me to use?
or is it ok to compare using the logical operators in comparing?
Oct 10 '06 #3
sorry sir i am not understanding this mem strcom diffrence
please give me a example
Oct 10 '06 #4
D_C
293 100+
Suppose a and b are pointers such that:
*a = 0x68002573
*b = 0x68003673

strcmp(a,b) returns equal because "D" equals "D".
memcmp(a,b,4) returns b > a, since the first and second bytes are the same, but the third byte of b is greater than the third byte of a. The rest of the bytes (just the 4th one) is unnecessary. If memcmp compares the four bytes from both strings, and they are the same, it will return b = a.

With strcmp, it goes until it finds a byte with value 0x00 in either string. You need to pass to memcmp a number of how many bytes to compare. It will compare that many bytes unless there is a difference between respective byte values.
Oct 10 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: rajkumar | last post by:
I have a struct like struct MyStruct { int a; int b; int c: bool d; bool e; }
4
by: blueblueblue2005 | last post by:
hi, I am reading the description of memcmp and strcmp, it seems there is no big difference between these two functions. except that memcmp takes void pointer parameters. so why string.h has these...
6
by: Sidney Cadot | last post by:
Hi all, Just browsing through my newly-acquired C99 spec, I was reading on the memcmp() function (7.21.4.1). It has a rather peculiar wording: int memcmp(const void *s1, const void *s2, size_t...
7
by: Alex Stark | last post by:
Hi, I'm new to c# an missing functions like memcmp and memcpy from C. Is there something similar ? cu Alex
4
by: Preets | last post by:
Hi, Can anybody please tell me the difference between memcmp() and strncmp() functions ?
3
by: Sonnich | last post by:
Hi! I have 2 arrays which I sort ny the first one. for($i=0;$i<(count($item1)-1);$i++) { $k = $i; for($j=$i;$j<(count($item1)-1);$j++); { if (strcmp($item1,$item1)<0)
3
by: gevadas | last post by:
sample program #include <iostream> #include <vector> using namespace std; int find(char*& value,char** arr,int size) { for(int i = 0;i < size;i++)
39
by: Martin | last post by:
Please consider the following code fragment. Assume UINT32 is a typedef suitable for defining variables of 32 bits, and that ui32 is initialised. UINT32 ui32; /* ... */ /* assume ui32 now is...
1
by: jawaraj1911 | last post by:
Hi all, Could any help me in which one of following will be efficient,using memcmp() or strcmp() in what way both are good. Thanks Jawahar raj R
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.