深入理解 C 指標閱讀筆記,深入理解指標

來源:互聯網
上載者:User

深入理解 C 指標閱讀筆記,深入理解指標

      上周末,我在圖書館看到了這本很薄的書 -- 《深入理解 C 指標


      這本書中寫的內容,個人感覺適合一個初學者,內容不是很難。我也讀了下,對每一章都做了筆記,筆記都是用代碼的形式貼出來。

Chapter1.h

#ifndef__CHAPTER_1_#define__CHAPTER_1_/*《深入理解C指標》學習筆記 -- 第一章*//*一個數如果是無符號的,那麼盡量去選用 size_t 類型*//*使用 size_t 類型的時候一定要用對了輸出格式*/void __size_t_test();//對指標進行加的操作就是指標原來指向的地址加上資料類型的大小乘以加的那個數//對指標進行減的操作就是指標原來指向的地址減去資料類型的大小乘以減的那個數void __pointer_add_sub_test();/*指標用 const 修飾的各種表現*/void __const_to_pointer();#endif

Chapter1.cpp

#include "Chapter1.h"#include <stdio.h>/*一個數如果是無符號的,那麼盡量去選用 size_t 類型*//*使用 size_t 類型的時候一定要用對了輸出格式*/void __size_t_test(){size_t test1 = -1;printf("test1 : %d\n", test1);printf("test1 : %u\n", test1);size_t test2 = 1;printf("test2 : %d\n", test2);printf("test2 : %u\n", test2);/*輸出結果如下:test1 : -1test1 : 4294967295test2 : 1test2 : 1*/}//對指標進行加的操作就是指標原來指向的地址加上資料類型的大小乘以加的那個數//對指標進行減的操作就是指標原來指向的地址減去資料類型的大小乘以減的那個數void __pointer_add_sub_test(){int test1 = 1;int* p_test1 = &test1;char test2 = 'a';char* p_test2 = &test2;printf("p_test1 - 1 address is : %x\n", p_test1 - 1);printf("p_test1 address is : %x\n", p_test1);printf("p_test1 + 1 address is : %x\n", p_test1 + 1);printf("\n");printf("p_test2 - 1 address is : %x\n", p_test2 - 1);printf("p_test2 address is : %x\n", p_test2);printf("p_test2 + 1 address is : %x\n", p_test2 + 1);/*輸出結果如下:p_test1 - 1 address is : 2dfdb8p_test1 address is : 2dfdbcp_test1 + 1 address is : 2dfdc0p_test2 - 1 address is : 2dfda6p_test2 address is : 2dfda7p_test2 + 1 address is : 2dfda8*/}void __const_to_pointer(){/*指向常量的指標*//*這裡的指向常量只是指標認為是指向常量的*/int test1 = 1;const int test2 = 2;/*這兩個操作都是可以的,但是不能解引用而修改它*/const int* p_test1 = &test1;const int* p_test2 = &test2;test1 = 3;      /*這個是可以的*///*p_test1 = 3;但是這個不行/*指向非常量的常量指標*/int test3 = 3;int* const p_test3 = &test3;/*指標只能指向非常量,因為可以用解引用修改變數的值*///p_test3 = &test1;同時,指標的值不能被修改//int* const p_test4 = &test2;指標只能指向一個非常量,因為可以被修改/*指向常量的常量指標*/const int* const p_test4 = &test2;/*代表指標的指向和指標指向的值都不能被修改*/}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.