简体   繁体   English

将会发生什么,我们将-1作为strncmp()中第3个参数的值传递。 n个数字字节要比较的字节数?

[英]What will happen we pass -1 as value for 3rd parameter in strncmp() ie. n number bytes of bytes to compare?

I have three questions, 我有三个问题,

  1. What will happen we pass -1 as value for 3rd parameter in strncmp() ie. 将会发生什么,我们将-1作为strncmp()中第3个参数的值传递。 n number bytes of bytes to compare? n个数字字节要比较的字节数?
  2. Why the output differs in ARM and PPC? 为什么输出在ARM和PPC中不同? ie. 即。 if which one correct? 如果哪一个正确?
  3. If I use memcmp instead of strncmp, I m getting "inside else" output in both ARM and PPC. 如果我使用memcmp而不是strncmp,那么我在ARM和PPC中都将获得“ inside else”输出。 How and why? 如何以及为什么?

     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"); } 

Output in ppc 以ppc输出

str: ,len:0
inside else

Output in arm 手臂输出

str: ,len:0
inside if

What will happen we pass -1 as value for 3rd parameter in strncmp() 会发生什么,我们将-1作为strncmp()中第三个参数的值传递

Assuming the 3rd parameter is defined a being of size_t and furthermore assuming size_t is defined as unsigned integer, passing in -1 will result in a "wrap-around" and the function will receiving the value of SIZE_MAX . 假设第三个参数定义为size_t ,并且还假设size_t被定义为unsigned整数,则传入-1将导致“环绕”,并且函数将接收SIZE_MAX的值。 On a 32bit system this probably would be 0xffffffff . 在32位系统上,这可能是0xffffffff

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM