<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          你搞清楚「NULL」、「0」、「'0'」、「"0"」「\0」了嗎?

          共 302字,需瀏覽 1分鐘

           ·

          2020-11-28 16:16


          我們先討論NULL,平時使用指針的時候,會經(jīng)常遇見這個家伙,這個家伙的值是是這樣定義的

          #define?NULL?0
          或者
          #define?NULL?(void?*)0

          我們看一下下面這段代碼

          #include?

          int?main?()?{
          ????size_t?ii;
          ????int?*ptr?=?NULL;
          ????unsigned?long?*null_value?=?(unsigned?long?*)&ptr;
          ????if?(NULL?==?0)?{
          ????????printf?("NULL?==?0\n");?}
          ????printf?("NULL?=?0x");
          ????for?(ii?=?0;?ii?????????printf?("%02X",?null_value[ii]);?}
          ????printf?("\n");
          ????return?0;
          }

          程序輸出

          NULL?==?0
          NULL?=?0x000061FE08000400B1138000

          --------------------------------
          Process?exited?after?0.02921?seconds?with?return?value?0
          請按任意鍵繼續(xù).?.?.

          我們用 &ptr 獲取 ptr的地址,然后用 unsigned long * 把這個地址轉(zhuǎn)換成 unsigned long *類型,之后再用 * 鑰匙來打開這個地址,獲取這個地址的值。只不過這里不是用 *獲取,用的是數(shù)組偏移。

          我們修改下代碼

          #include?

          int?main?()?{
          ????size_t?ii;
          ????int?*ptr?=?NULL;
          ????unsigned?long?*null_value?=?(unsigned?long?*)&ptr;
          ????if?(NULL?==?0)?{
          ????????printf?("NULL?==?0\n");?}
          ????printf?("NULL?=?0x");
          ????for?(ii?=?0;?ii?????????printf?("%02X",?null_value[ii]);?}
          ????printf?("\n");
          ????printf("0x%p?0x%p\n",ptr,&ptr);
          ????return?0;
          }

          程序輸出

          NULL?==?0
          NULL?=?0x000061FE08000400A7138000
          0x0000000000000000?0x000000000061FE08

          --------------------------------
          Process?exited?after?0.03177?seconds?with?return?value?0
          請按任意鍵繼續(xù).?.?.

          這樣看之后,就覺得沒有那么拗口了吧。

          我們再討論下 「'0'」

          字符 0 和其他都不能混為一談,它是一個字符,字符對應(yīng)的是ascii 碼

          #include?

          int?main?()?{
          ?char?c?=?'0';?
          ?printf("'%c'?0x%x?%d\n",c,c,c);?
          ????return?0;
          }

          程序輸出

          '0'?0x30?48

          --------------------------------
          Process?exited?after?0.02841?seconds?with?return?value?0
          請按任意鍵繼續(xù).?.?.

          「"0"」是一個字符串,字符串跟字符的不同是,字符串在最后面有一個字符結(jié)束標(biāo)識 nul

          測試程序

          #include?
          #include?
          int?main?()?{
          ?char?*?str?=?"0";?
          ?int?len?=?strlen(str);
          ?printf("%d\n",len);?
          ?for(int?i?=?0;i??printf("[0x%x]",str[i]);
          ????return?0;
          }

          程序輸出

          1
          [0x30][0x0]
          --------------------------------
          Process?exited?after?0.02936?seconds?with?return?value?0
          請按任意鍵繼續(xù).?.?.

          「\0」這個就有點意思了,這個其實也就是數(shù)值 0

          測試程序

          #include?
          #include?
          int?main?()?{
          ?char?c?=?'\0';
          ?printf("'%c'?0x%x?%d\n",c,c,c);?
          ????return?0;
          }

          程序輸出

          '?'?0x0?0

          --------------------------------
          Process?exited?after?0.03893?seconds?with?return?value?0
          請按任意鍵繼續(xù).?.?.

          好了,就這些,看了這些之后,就再也不用擔(dān)心如果一個字符串里面有 0 字符不知道怎么做算法比較了吧。


          推薦閱讀:專輯|Linux文章匯總專輯|程序人生專輯|C語言


          嵌入式Linux微信掃描二維碼,關(guān)注我的公眾號?


          瀏覽 23
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  18成人网站在线观看 | 久久久免费精品国产夜色 | 五月停停欧美激情 | 国产日本视频完整版无删减在线观看 | 成人AV三级 |