我有三个问题
- 我们将 -1 作为 strncmp() 中第三个参数的值传递,会发生什么。要比较的字节数是 n 个字节?
- 为什么 ARM 和 PPC 的输出不同? IE。如果哪一个是正确的?
如果我使用 memcmp 而不是 strncmp,我会在 ARM 和 PPC 中获得“inside else”输出。如何以及为何?
char str[10]; memset(str,'\0',sizeof(str)); printf("str:%s ,len:%d\n\r",str,strlen(str)); if(strncmp(str,"Maximum",(strlen(str)-1)) == 0) { printf("inside if\n\r"); } else { printf("inside else\n\r"); }
以 ppc 形式输出
str: ,len:0
inside else
ARM 输出
str: ,len:0
inside if
最佳答案
What will happen we pass -1 as value for 3rd parameter in strncmp()
假设第三个参数定义为 size_t
,并且进一步假设 size_t
定义为 unsigned
整数,传入 - 1
将导致“环绕”,并且该函数将接收 SIZE_MAX
的值。在 32 位系统上,这可能是 0xffffffff
。
关于c - 我们将 -1 作为 strncmp() 中第三个参数的值传递,会发生什么。 n 要比较的字节数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31743944/