首页 > 代码库 > c的指针(浅见)

c的指针(浅见)

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 */