C語言的儲存(2)

來源:互聯網
上載者:User

在上一篇筆記裡講述裡c語言裡與儲存相關的知識,下面通過代碼再說明一下,看的就比較清楚了.
注意:下面的代碼只是在說明文法,並不是真的寫程式,寫程式要在學習完函數之後才能開始.
下周一(12.14),二,四晚上18:10  直播課堂零基礎學c語言開課,對學java,c#的同學一樣有用,歡迎感興趣的朋友到時參加 

Code:
  1. // cstudy.cpp : 定義控制台應用程式的進入點。   
  2. //   
  3. #include <stdio.h>   
  4. struct ball   
  5. {   
  6.     int x;   
  7.     int y;   
  8.     bool haveBall;   
  9.     int ballColor;   
  10. };   
  11. int main(int argc, char* argv[])   
  12. {   
  13.     printf("hello!/n");   
  14.   
  15.     int a  ;     //開空間 ,佔4個位元組    
  16.     a=258  ;     //258 儲存到 空間 a,  分別是 02,01,00,00   
  17.     int *p ;     //開指標空間p   
  18.     p=&a;        //a的地址存到p   
  19.     *p=*p+1;   //或(*p)++, p指向a,相當於a++;   
  20.     printf("指標示範,通過p顯示a的值是%d/n",*p);   
  21.   
  22.     //示範類型轉換,即不同類型空間賦值   
  23.     char c;   
  24.     int m;   
  25.     c=65;//或c='A';   
  26.     m=c; //m的空間大於c,所以沒有任何問題;   
  27.     c=(char)a;//a的空間大於c,所以要強制轉換,同時有資料截斷   
  28.     printf("c的值只能是a的最低位位元組,所以c=%d/n",c);//數字2代表的asc碼看不到,用整數顯示   
  29.   
  30.   
  31.     //字串和字元數組   
  32.     char str[]="This is string!";   
  33.     char str1[]={'T','h','e',32,'e','n','d'};   
  34.     printf("這個是數組:%s/n",str);   
  35.     printf("這個不是數組:%s/n",str1);   
  36.   
  37.   
  38.     //數組和指標,指標數組,雙重指標:   
  39.     char s1[]="This is first array!";   
  40.     char s2[]="This is second array!";   
  41.     char s3[]="This is third array!";   
  42.     char *sp[3];   
  43.     char **pp;   
  44.     sp[0]=s1;   
  45.     sp[1]=s2;   
  46.     sp[2]=s3;   
  47.     pp=sp;   
  48.     printf("測試指標數組:%s|%s|%s/n",*pp,*pp++,sp[2]);   
  49.   
  50.   
  51.      //指標類型轉換和指標運算   
  52.     char *cp;   
  53.     cp=(char *)p;// p是int指標,p指向a,a的值是259   
  54.     printf("cp也指向a,但*cp只能訪問一個位元組,所以*cp的值是%d/n",*cp);   
  55.     cp++;   
  56.     printf("指向下一個位元組是%d/n",*cp);   
  57.     cp++;   
  58.     printf("指向下一個位元組是%d/n",*cp);   
  59.     cp++;   
  60.     printf("指向下一個位元組是%d/n",*cp);   
  61.     // 此時p++不能使用,因為a不是數組裡的空間,只有數組才能保證連續的   
  62.   
  63.   
  64.     //結構體樣本   
  65.     ball ba;   
  66.     ball *pba;   
  67.     pba=&ba;   
  68.     ba.x=3;   
  69.     (*pba).y=5;   
  70.     ba.haveBall=true;   
  71.     pba->ballColor=2;   
  72.     printf("ba存了一個小球的資料,座標是(%d,%d),有球,顏色是%d/n",pba->x,pba->y,pba->ballColor);   
  73.   
  74. }   
  75.   

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.