• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            C++分析研究  
            C++
            日歷
            <2016年3月>
            282912345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789
            統計
            • 隨筆 - 92
            • 文章 - 4
            • 評論 - 4
            • 引用 - 0

            導航

            常用鏈接

            留言簿

            隨筆檔案

            文章檔案

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

             

            有時候錯誤的代碼會讓我們更深刻的理解一門編程語言,所以在Learn c the hard way一書中,作者不停的讓我們把正確的代碼破壞掉,發揮我們的好奇心與想象力,去發覺c語言的一些特性。
            為了弄清弄清c中int與char型變量存儲的異同和gcc對無'\0'字符數組的的反應,修改了Learn c the hard way書中的ex8
            代碼如下
            #include <stdio.h>
            int main(int argc,char *argv[])
            {
            int areas[] = {10, 12, 13, 14, 20};
            char name [] = "Zed";
            char full_name[] = {
            'Z', 'e', 'd',
            ' ', 'A', '.', ' ',
            'S', 'h', 'a', 'w'
            };
            unsigned int interg[2];
            interg[0] = '\0' * 0x01000000 + 'A'*0x00010000 + 'B' * 0x00000100 + 'C' * 0x00000001;
            unsigned int inter = interg[0];
            // WARNING: On some system you may have to change the
            // %ld in this code to a %u since it will use unsignt ints
            printf("The size of an int: %ld\n",sizeof(int));
            printf("The siez of areas (int[]):%ld\n",
            sizeof(areas));
            printf("The number of ints in areas: %ld\n",
            sizeof(areas)/sizeof(int));
            printf("The first area is %d, the 2nd %d.\n",
            areas[0],areas[10]);
            printf("The size of a char: %ld\n",sizeof(char));
            printf("The size of name (char[]): %ld\n",
            sizeof(name)/sizeof(char));
            printf("The size of full_name (char[]): %ld\n",
            sizeof(full_name));
            printf("The number of chars: %ld\n",
            sizeof(full_name) / sizeof(char));
            // !!!其中full_name未設置null結尾
            printf("name=\"%s\" and full_name=\"%s\"\n",
            name, full_name);
            // test 證明了gcc把未初始化的地方初始為0
            printf("the char after the full_name(shuold be error):%c\n",full_name[12]);
            // 用 int 存儲字符串
            printf("interg=%X\n", interg);
            printf("inter=\"%s\"\n", &inter);
            printf("interg=\"%s\"\n", interg);
            return 0;
            }
            重點在于
            char full_name[] = {
            'Z', 'e', 'd',
            ' ', 'A', '.', ' ',
            'S', 'h', 'a', 'w'
            };
            unsigned int interg[2];
            interg[0] = '\0' * 0x01000000 + 'A'*0x00010000 + 'B' * 0x00000100 + 'C' * 0x00000001;
            unsigned int inter = interg[0];
            可以看到full_name字符串初始化的時候是沒有null結尾的,如果的vc中編譯并以字符串形式輸出的話會跟上亂碼
            interg的數組第一個元素中我分別在四個字節中放入了'\0'(即0)、'A'、'B'、'C',第二個元素未初始化。
            inter中放的是同樣的數字。
            輸出如下:
            The size of an int: 4
            The siez of areas (int[]):20
            The number of ints in areas: 5
            The first area is 10, the 2nd -1493946707.
            The size of a char: 1
            The size of name (char[]): 4
            The size of full_name (char[]): 11
            The number of chars: 11
            name="Zed" and full_name="Zed A. Shaw"
            the char after the full_name(shuold be error):
            interg=6EB11E70
            inter="CBA"
            interg="CBA"
            可以看到以字符串輸出的full_name并沒有因為無null結尾而輸出亂碼或錯誤,在csdn論壇上了解到可能是因為gcc將字符串存儲區全部初始化為零。
            PS:對于數組的初始化,可以只初始化一個元素,就可以使其他元素為零(Learn c the hard way);
            inter以字符串輸出的結果是按我賦值的倒序輸出的,這個是因為小端的數據存儲造成的托福答案

            posted on 2014-08-25 21:15 HAOSOLA 閱讀(318) 評論(0)  編輯 收藏 引用
             
            Copyright © HAOSOLA Powered by: 博客園 模板提供:滬江博客
            PK10開獎 PK10開獎
            99久久免费国产精品热| 亚洲国产香蕉人人爽成AV片久久 | 国内精品久久久久久野外| 香蕉久久夜色精品国产小说| 久久久久国产一级毛片高清板| 狠狠色丁香久久婷婷综合蜜芽五月| 亚洲精品无码久久千人斩| 99久久精品国产综合一区| 亚洲美日韩Av中文字幕无码久久久妻妇| 中文字幕久久精品无码| 66精品综合久久久久久久| 久久久久久久久久久| 国产精品欧美亚洲韩国日本久久| 久久久久久久久久久| 日韩中文久久| 国内精品伊人久久久久影院对白 | 色99久久久久高潮综合影院| 国产精品久久久久aaaa| 亚洲va久久久噜噜噜久久男同| 久久夜色撩人精品国产| 九九99精品久久久久久| 日本人妻丰满熟妇久久久久久| 香蕉久久AⅤ一区二区三区| 青青草原1769久久免费播放| 91精品国产9l久久久久| 无码专区久久综合久中文字幕 | 久久综合九色综合网站| 久久精品国产色蜜蜜麻豆| 久久青草国产手机看片福利盒子| 免费久久人人爽人人爽av| 亚洲AⅤ优女AV综合久久久| 国产成人精品久久一区二区三区av| 国产美女久久精品香蕉69| 欧美精品久久久久久久自慰| 77777亚洲午夜久久多喷| 日日噜噜夜夜狠狠久久丁香五月 | 蜜臀av性久久久久蜜臀aⅴ| 亚洲AV日韩AV天堂久久| 99蜜桃臀久久久欧美精品网站| 亚洲精品无码久久千人斩| 久久精品九九亚洲精品|