Introduction to the basic knowledge of C language Learning (10): Memory space model, address interpretation and pointer variables

Source: Internet
Author: User

One, memory
/*   Memory:          exists in memory.  memory is divided into more than n small spaces, each small space 1 bytes  Each small space has its own address. A difference of 1 int between each address  takes 4 bytes, equals 4 spaces (4 addresses), does not need to remember 4 addresses, and remembers the first address (because the first address is equivalent to the entrance or the door in life)  :     The pointer is the address, and the address is the pointer. Address: Address in memory address   : In fact,        it is numbered from 1, and then has been self-increasing        1 m of memory, 1024 address  0x7fff5fbff7cc: This is called 16  decimal: every 10 into 1        There are: 0  1  2  3  4  5  6  7  8  9                       every ten into 1                                9  Hex: Just 16 in 1                       there: 0  1  2 3 4  5  6  7  8  9  a  b  c  d  e  f  start with 0x          Simple said 1-10                0x1   0x2  0x3  0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf            16:0x10 0x35:53  0xa0:160 in  passing:    binary: 0 and 1            0   1    octal: 0 1 2 3 4 5 6 7            */
Second, pointer variable
/* pointer variable: The variable that holds the address.        Syntax: type  * pointer variable name;        Assignment: pointer variable name  = Address;     Initialize at the time of declaration: type  * pointer variable name = address;       For example: int num = 20;int *nump = #//The address of NUM is the print pointer variable:         printf ("%p \ n \ n",%p); Note: There are several ways to define pointer variables:            1, type  *  variable name;            2, type *  variable name;            3, type  * variable name, the above three kinds can be, *  represents a special type. Type *  forms the type * * 

The above describes how to define a pointer variable, and here's how to use it.

/*use pointer variable syntax: * pointer variable name;            Semantics: This gives you access to the values in the space you're pointing to. It is important to note that there is an asterisk and no asterisk difference when accessing pointer variables: int num;             int *p = # 1. An asterisk represents a value that accesses the space it points to.             *p equivalent to Num; 2. Without an asterisk, it is a value that accesses its own space. P is the value stored in itself.*///For example:    intnum =Ten; int*Pnum; Pnum= #//note:& do not omit! printf ("%d%d\n", Num,*pnum);//so that is to say, *pnum = = num;*pnum = -; printf ("num=%d\n", num);// -Num= -; printf ("*pnum=%d\n",*pnum); intnum =Ten; int*pnum = # (*pnum) + +;//equivalent to num++ num=11Num+= -; printf ("num=%d\n", num);// to        intnum =Ten; int*pnum = # int*pnum2 =Pnum; *pnum2 = -; printf ("num=%d *pnum=%d*pnum2=%d\n", num,*pnum,*pnum2);//30    

It is important to note that when using pointer variables, the asterisking (*) is different from the asterisk (*), and the pointer variable also distinguishes the type (the type is to tell the pointer, which reads a few bytes).

What type of variable is used to read by what type of pointer.

Let's look at an example:

intNUM1 =Ten, num2 = -;int*P1 = &NUM1;int*P2 = &num2;int*P3 =P2;*P2 = +;p 2=P1;*P2 = -;p rintf ("Num1 =%d num2 =%d *p1=%d *p2=%d *p3=%d", num1,num2.*p1,*p2,*p3);/*you can count on yourself if you are interested. */

Pointer variables as arguments to functions:

/* Syntax: Return value type function name (pointer type *  pointer variable name) {     } for example: void Test1 (int*  num) {    }int main (int argc,const char argv[]) {     int num = ten;     Test1 (&num);} */

Introduction to the basic knowledge of C language Learning (10): Memory space model, address interpretation and pointer variables

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.