C語言入門-指標入門

來源:互聯網
上載者:User

指標概述
          所有的變數都會在記憶體空間分配一塊記憶體,指標是用來表示這塊記憶體空間的地址的,可以用取地址符&來擷取一個變數在記憶體空間的地址,如果想存放一個地址,就可以用一個指標變數來存放,存放記憶體位址的變數就是指標變數     指標是用來表示一塊記憶體位址的,指標變數是用來存放記憶體位址的     
#include <stdio.h>#include <stdlib.h>main(){      int i = 10;      int* p = &i;      printf("%#x\n",p);           //p = NULL;      //printf("i=%d\n",i);      //i=10           //printf("*p=%d\n",*p);      //*p=10           //*p = 100;      //printf("i=%d\n",i);      //i=100           i = 90;      printf("*p=%d\n",*p);      //*p=90           /**      總結         p是一個指標變數,儲存的是i的記憶體位址         *p和i其實代表的是同一塊記憶體空間           */      system("pause");}
          值的交換
 #include <stdio.h>

#include <stdlib.h>
//形參i,j等於是重新開闢了兩塊記憶體位址更跟實參i,j已經沒有什麼關係了
void swap(int i,int j){
      int temp;
      temp = j;
      j = i;
      i = temp;
     
}
//p,q和i,j代表的都是同一塊記憶體位址
void swap_pointer(int* p,int* q){
     int temp;
     temp = *q;
     *q = *p;
     *p = temp;
}
main()
{
     
     
      int i = 10;
      int j = 30;
     
      printf("交換前...\n");
      printf("i=%d\n",i);
      printf("j=%d\n",j);
     
      //swap(i,j);
      swap_pointer(&i,&j);
      
      printf("交換後...\n");
      printf("i=%d\n",i);
      printf("j=%d\n",j);
      system("pause");
}


指標當中的常見錯誤     

 #include <stdio.h>

#include <stdlib.h>
main()
{
     
      /**垃圾指標,野指標*/
      //int* p;//p還未指向任何記憶體空間,不能夠被使用
      //printf("*p=%d\n",*p);
      //*p = 2425;
     
      /**類型的不同的指標不可以相互轉換*/
      int i = 9999;
      char* c;
      c = &i;
      printf("*c=%c\n",*c);
      
      system("pause");
}

      不可使用已回收掉了的指標變數
 #include <stdio.h>

#include <stdlib.h>
//函數完成時i將會被回收
void func(int** q){//代表的是指標變數的指標變數
     int i = 3;
     *q = &i;//*q代表的是指標變數p    
}
main()
{
      int* p;
      printf("p的記憶體位址為%#x\n",p);
     
      printf("i=%d",*p);//註:有可能在i還未被回收時擷取到值
      system("pause");
}

  通過傳遞指標變數讓子函數修改主函數中變數的值 
#include <stdio.h>#include <stdlib.h>//可以通過傳遞指標變數的方式,在子函數中修改主函數中變數的值void func(int* p,int* q){     *p = 100;     *q = 200;}main(){           int i = 10;      int j = 20;           func(&i,&j);           printf("i=%d\n",i);      printf("j=%d\n",j);           system("pause");}

字串和數組


#include <stdio.h>

#include <stdlib.h>
/**
數組是一塊連續的記憶體空間
數組的名字代表的是數組的首地址和arr[0]的記憶體位址是一樣的
後續元素的記憶體位址都是在首地址的基礎上後移
arr[0] == *(arr+1)
*/
//列印數組中的每一個元素
void printArr(int* arr,int len)
{
     int i=0;//c99版本中必須在外面進行初始化
     for(;i<len;i++)
     {
        printf("arr[%d]=%d\n",i,*(arr+i));       
     }
}

main()
{
      //字串數組
      char carr[20] = {'a','b','c','\n'};
      printf("carr=%s\n",carr);
      printf("carr=%#X\n",&carr);
      printf("&carr[0]=%#X\n",&carr[0]);
      printf("carr[1]=%c\n",*(carr+1));
     
      //char指標類型來代替字串
      //char* arr1 = "abc\n";
      //printf("arr1=%s",arr1);
     
      int  arr[5] = {1,2,3};
      printf("arr=%#x\n",arr);
      printf("&arr[0]=%#X\n",&arr[0]);
      printf("arr[1]=%d\n",*(arr+1));
      
      printArr(arr,5);
      system("pause");
}

指標的運算


#include <stdio.h>#include <stdlib.h>/**   指標的運算並不是向上或向下移動一個位元組   而是按照約定的類型,移動約定的類型的位元組數*/main(){      //char類型在記憶體中佔一個位元組      char charArr[4] = {'a','b','c','d'};      char* cp = &charArr[2];      printf("*(cp-1)=%c\n",*(cp-1));           //int類型在記憶體中佔4個位元組      int intArr[4] = {1,2,3,4};      int* ip = &intArr[2];      printf("*(ip-1)=%d\n",*(ip-1));           system("pause");}

指標佔多少個位元組

#include <stdio.h>#include <stdlib.h>main(){      int i = 2;      float f = 2;      double d = 2;      char c = 'b';           int* ip = &i;      float* fp = &f;      double* dp = &d;      char* cp = &c;           printf("int指標占的長度為%d\n",sizeof(ip));       printf("float指標占的長度為%d\n",sizeof(fp));      printf("double指標占的長度為%d\n",sizeof(dp));      printf("char指標占的長度為%d\n",sizeof(cp));      /**      int指標占的長度為4      float指標占的長度為4      double指標占的長度為4      char指標占的長度為4      */           /**           32位作業系統最大表示的記憶體空間為2的32次方      指標只需要4位 就可以表示出來所有的記憶體空間           64位作業系統,當編譯器支援64位時,指標所佔的長度為8byte      */      system("pause");}
函數指標
 #include <stdio.h>

#include <stdlib.h>
int add(int x,int y)
{
    printf("x+y=%d\n",x+y);
}
main()
{
      //函數指標
      int (*pFunc) (int x,int y);
      pFunc = add;
      pFunc(4,9);
      system("pause");
      return 0;
}

聯繫我們

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