c的指標(淺見)

來源:互聯網
上載者:User

標籤:style   blog   color   使用   os   io   資料   for   

1.變數:一段儲存地區,每個變數都有一個地址,指向變數的儲存位置.
  變數名:變數名對應變數的地址.(編譯器會將程式中的變數地址和變數名,建立一個"符號表",編寫代碼時對變數的操作,實際編譯器會拿 符號表中變數名對應的
地址中儲存的資料進行操作.)

2.指標是個變數,變數裡面儲存的值用來當作地址.
  char *p;
  p是變數,儲存的值是一個地址.(p本身也有地址,&p)

3.二維指標:
  char **p;
  p儲存的是一個指標的地址.(p本身也有地址,&p)


4.指標和數組
char a[10];
char *p;
區別: a == &a;
    p != &p;

5.u/cos ii 的指標的運用.

OS_MEM  *OSMemCreate (void   *addr,                      INT32U  nblks,                      INT32U  blksize,                      INT8U  *perr){    OS_MEM    *pmem;    INT8U     *pblk;    void     **plink;    INT32U     loops;    INT32U     i;    // ...    OS_ENTER_CRITICAL();    pmem = OSMemFreeList;                             /* Get next free memory partition                */    if (OSMemFreeList != (OS_MEM *)0) {               /* See if pool of free partitions was empty      */        OSMemFreeList = (OS_MEM *)OSMemFreeList->OSMemFreeList;    }    OS_EXIT_CRITICAL();    if (pmem == (OS_MEM *)0) {                        /* See if we have a memory partition             */        *perr = OS_ERR_MEM_INVALID_PART;        return ((OS_MEM *)0);    }    plink = (void **)addr;                            /* Create linked list of free memory blocks      */    pblk  = (INT8U *)addr;    loops  = nblks - 1u;    for (i = 0u; i < loops; i++) {        pblk +=  blksize;                             /* Point to the FOLLOWING block                  */       *plink = (void  *)pblk;                        /* Save pointer to NEXT block in CURRENT block   */        plink = (void **)pblk;                        /* Position to  NEXT      block                  */    }    *plink              = (void *)0;                  /* Last memory block points to NULL              */    pmem->OSMemAddr     = addr;                       /* Store start address of memory partition       */    pmem->OSMemFreeList = addr;                       /* Initialize pointer to pool of free blocks     */    pmem->OSMemNFree    = nblks;                      /* Store number of free blocks in MCB            */    pmem->OSMemNBlks    = nblks;    pmem->OSMemBlkSize  = blksize;                    /* Store block size of each memory blocks        */    *perr               = OS_ERR_NONE;    return (pmem);}

 

摘取一段:

    plink = (void **)addr;                            /* Create linked list of free memory blocks      */    pblk  = (INT8U *)addr;    loops  = nblks - 1u;    for (i = 0u; i < loops; i++) {        pblk +=  blksize;                             /* Point to the FOLLOWING block                  */       *plink = (void  *)pblk;                        /* Save pointer to NEXT block in CURRENT block   */        plink = (void **)pblk;                        /* Position to  NEXT      block                  */    }    *plink              = (void *)0;                  /* Last memory block points to NULL              */

其中

plink = (void **)addr; pblk = (INT8U *)addr;

乍一看,很高深的樣子,addr本身是一維的,怎麼既作為一維指標,又作為二維指標使用.

但看看後面plink的使用,plink只是取一維的深度來用,也就是說等價以下代碼:

void *plink;plink = (void *)addr; /* Create linked list of free memory blocks */pblk = (INT8U *)addr;loops = nblks - 1u;for (i = 0u; i < loops; i++) {  pblk += blksize; /* Point to the FOLLOWING block */  *plink = (void *)pblk; /* Save pointer to NEXT block in CURRENT block */  plink = (void *)pblk; /* Position to NEXT block */}*plink = (void *)0; /* Last memory block points to NULL */

 

相關文章

聯繫我們

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