Pointer to C (for more information, see)

Source: Internet
Author: User

1. Variable: a storage area. Each variable has an address pointing to the storage location of the variable.
Variable name: the address of the variable corresponding to the variable name. (The Compiler creates a "symbol table" for the variable address and variable name in the program, and operates on the variable when writing the code. The actual compiler will take the variable name corresponding to the symbol table
The data stored in the address .)

2. the pointer is a variable. The value stored in the variable is used as an address.
Char * P;
P is a variable and the stored value is an address. (P also has an address, & P)

3. Two-Dimensional pointer:
Char ** P;
P stores the address of a pointer. (P also has an address, & P)


4. pointers and Arrays
Char A [10];
Char * P;
Difference: A ==&;
P! = & P;

5. Usage of U/cos ii pointers.

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);}

 

Extract A segment:

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

Where

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

At first glance, the ADDR itself is one-dimensional. How can it be used as both a one-dimensional pointer and a two-dimensional pointer.

But let's look at the use of plink later. plink is used only for the depth of one dimension. That is to say, it is equivalent to the following code:

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.