- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我用 C 编写了这段小代码来测试 C 中的 memcmp()
strncmp()
strcmp()
函数。
这是我写的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *word1="apple",*word2="atoms";
if (strncmp(word1,word2,5)==0)
printf("strncmp result.\n");
if (memcmp(word1,word2,5)==0)
printf("memcmp result.\n");
if (strcmp(word1,word2)==0)
printf("strcmp result.\n");
}
因为我对这三个函数感到困惑,有人能解释一下它们之间的区别吗?
我的主要问题是我有一个文件,我在其中标记了它的行,问题是当我标记文件中的单词“atoms”时,我必须停止标记化过程。
我首先尝试了 strcmp()
但不幸的是,当它到达文件中放置单词“atoms”的位置时它并没有停止并继续,但是当我使用memcmp()
或 strncmp()
它停止了,我很高兴。
但后来我想,如果有一个字符串的前 5 个字母是 a、t、o、m、s,并且后面跟着其他字母,那会怎么样。
不幸的是,我的想法是正确的,因为我使用上面的代码测试了它,将 word1
初始化为“atomsaaaaa”,将 word2
初始化为 atoms 和 memcmp()
和 strncmp()
在 if 语句中返回 0。另一方面 strcmp()
它没有。看来我必须使用strcmp()
。
最佳答案
简而言之:
所以,如果你有这些字符串:
const char s1[] = "atoms\0\0\0\0"; // extra null bytes at end
const char s2[] = "atoms\0abc"; // embedded null byte
const char s3[] = "atomsaaa";
那么这些结果成立:
strcmp(s1, s2) == 0 // strcmp stops at null terminator
strcmp(s1, s3) != 0 // Strings are different
strncmp(s1, s3, 5) == 0 // First 5 characters of strings are the same
memcmp(s1, s3, 5) == 0 // First 5 bytes are the same
strncmp(s1, s2, 8) == 0 // Strings are the same up through the null terminator
memcmp(s1, s2, 8) != 0 // First 8 bytes are different
关于c - C中的memcmp、strcmp和strncmp有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13095513/
template int custom_memcmp(const T* a, const T* b, std::size_t n); 这会比 C 的 memcmp 更快吗? 如果 sizeof(T)
如果我为 memcmp 的第一个和第二个参数传递相等的指针,我怀疑它可能只是返回 0 而不检查元素——因为如果传递相同的指针,则元素必须为零。在我看来,检查指针相等性并提前退出是很好的优化。 我检查了
是 if(strncmp(buf, buf2, 7) == 0) 做同样的事情 if(memcmp(buf, buf2, 7) == 0) buf 和 buf2 是 char* 数组或类似数组。 我打
我从 fasm(版本 1.71.51)代码调用 memcmp,并且得到了奇怪的结果。 看起来memcmp只比较奇数位置的字符。代码: format ELF64 section '.text' exec
我想知道 memcmp 函数必须返回什么。 我一直在 Internet 上搜索,通常 memcmp 定义如下所示: The memcmp() function returns an integer g
如果我发送 memcmp 两个指向整数的指针,那么它似乎将整数解释为字符。 例如: int a = 5; int b = 256; int res = memcmp(&a,&b,sizeof(int)
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
struct Flat { int a1; int a2; } // a hierarchical struct which containing a struct attribute str
我正在使用一个包含许多成员的大型结构,我想要一种简单的方法来快速查看是否有任何成员非零。我知道 memcmp() 不应该用于比较两个结构是否相等(如下所述: How do you compare st
我想以最快速有效的方式找出两个内存缓冲区(保存任意定义的值)在按位比较中是否相同。 我对 bool 值“相同”以外的任何东西都不感兴趣,我希望该方法尽快返回,即首先发现差异。 实现此目标的最佳方法是什
我在使用 memcmp 时遇到了一个小问题。我有两个数组(长度 = 3 字节),数据完全相同。 如果我尝试将它们与 memcmp 进行比较,它会失败吗?! if (memcmp(ucbuffer, u
在 C 中,我想检查给定的字符数组中的任意字母,并根据它的内容进行更改。例如,字符“a”或“A”将更改为“4”(表示 4 的字符)。这是我的编码练习:) 代码如下: #include #includ
我有两个相同大小的 unsigned char 数组和一个检查它们是否相等的 if 语句: #define BUFFER_SIZE 10000 unsigned char origCh
我有一个缓冲区,还有几个指向它的指针。我想根据指针指向的缓冲区中的字节对指针进行排序。 qsort() 和 STL::sort() 可以被赋予自定义比较函数。例如,如果缓冲区是零终止的,我可以使用 s
这个问题有点难以解释,因为代码片段是一个更大项目的一部分。我会尽我所能解释这个问题。 我有两个文件 FILE *f,*m; f=fopen("/home/machine/decoder
我刚好调试了一个令人难以置信的严重错误:在我自己的 PC(Windows 7 x64,MinGw)上,我的 C 程序在比较数组成员时使用 memcmp 成功地对数组进行了排序。 我的函数使用了冒泡排序
在 C++ 中,比较两个字大小结构(例如 32 位架构中的 4 字节大小)的最有效方法(内存和时序)是什么。假设没有垃圾填充位并且: struct A, B; 一方面,我可以使用 memcmp(&A,
下面是memcmp的微软CRT实现: int memcmp(const void* buf1, const void* buf2, size_t count
下面的简单程序对我来说是段错误: #include int main() { void* voidp = NULL; char zeroes[sizeof(void*)];
我已经使用 memcmp 函数比较了两个字符串文字。 #include #include int main() { char str1[] = "abcd"; char str2[] =
我是一名优秀的程序员,十分优秀!