![](/img/trans.png)
[英]Extract 2nd and 3rd value from char array and cast it as short (2 bytes). In C
[英]What will happen we pass -1 as value for 3rd parameter in strncmp() ie. n number bytes of bytes to compare?
我有三個問題,
如果我使用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
手臂輸出
str: ,len:0
inside if
會發生什么,我們將-1作為strncmp()中第三個參數的值傳遞
假設第三個參數定義為size_t
,並且還假設size_t
被定義為unsigned
整數,則傳入-1
將導致“環繞”,並且函數將接收SIZE_MAX
的值。 在32位系統上,這可能是0xffffffff
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.