Dark Horse programmer--c Language Basic Grammar (ii)

Source: Internet
Author: User

   pointer variables, like ordinary variables, need to be defined not only by the description, but also by a specific value. An unassigned pointer variable cannot be used, otherwise it will cause system confusion and even panic. The assignment of a pointer variable can only be assigned to an address and must never be given any other data, or it will cause an error. In the C language, the address of a variable is assigned by the compilation system, completely transparent to the user, the user does not know the specific address of the variable。 Two related operators: &: Take the address operator. *: pointer operator (or "indirect access" operator). The address operator & is provided in the C language to represent the address of the variable. The general form is the:& variable name, and if &a represents the address of variable A, &b represents the address of the variable B. The variable itself must be described in advance. There are two ways to assign the address of an integer variable A to p if you have a pointer variable p that points to an integer variable: 1. How to initialize pointer variablesint A; int *p=&a; 2, the method of assigning a value statementint A;int *p;p=&a;It is not allowed to assign a number to a pointer variable, so the following assignment is wrong:int *p;p=1000;The "*" specifier cannot be added before the assigned pointer variable, such as *p=&a is also wrong. Assumptions:int i=200, x;int *ip;We have defined two integer variable i,x and defined a pointer variable IP that points to an integer number. An integer can be stored in the i,x, and only the address of an integer variable is stored in the IP. We can assign the address of I to ip:ip=&i;. At this point the pointer variable IP points to the integer variable i, assuming that the address of the variable i is 1800, the assignment can be visually understood as the contact shown. In the future, we can indirectly access the variable i through the pointer variable IP, for example: X=*IP; operator * accesses an IP-address storage area, and the IP holds the address of the variable I, so *ip accesses the storage area with address 1800 (because it is an integer, which is actually two bytes starting from 1800 ), which is the storage area occupied by I, so the above assignment expression is equivalent to x=i;In addition, pointer variables, like general variables, can be changed by the values stored in them, meaning they can be changed to point, assumingint i,j,*p1,*p2;i= ' a ';j= ' B ';p1=&i;p2=&j;The link is established as shown.  The assignment expression: P2=p1 causes P2 and P1 to point to the same object I, when *P2 is equivalent to I, not J, as shown in the figure. If the following expression is executed: *P2=*P1, the content that the P1 points to is assigned to the area referred to by P2, and it becomes the figure shownaccessing a variable pointed to by a pointer is done in the form of an indirect access, so it takes time and is not intuitive to access a variable directly, because which variable to access by the pointer depends on the value of the pointer (that is, the pointing), such as "*P2=*P1;" In fact, "J=i," the former is not only slow and unclear. But because pointers are variables, we can indirectly access different variables by changing their direction, which gives the programmer flexibility and makes the program code more concise and efficient. Pointer variables can appear in an expression, set int x,y,*px=&x; pointer variable px to an integer x, then *px can appear anywhere x can appear.  For example: y=*px+5;  /* means adding 5 to the contents of X and assigning it to y*/y=++*px;  /*PX content plus 1 is assigned to Y,++*PX equivalent to + + (*PX) */y=*px++; /* equivalent to Y=*PX; px++*/main() {  int a,b; int *pointer_1, *pointer_2; a=+; b=ten; pointer_1=&a; pointer_2=&b; printf("%d,%d\n",a,b); printf("%d,%d\n", *pointer_1, *pointer_2); }Description of the program: at the beginning, although two pointer variables pointer_1 and pointer_2 are defined, they do not point to any integer variable. Just provide two pointer variables, which specify that they can point to integer variables. The function of the 5th and 6 lines of the program is to make pointer_1 point a,pointer_2 to B. The *pointer_1 and *pointer_2 of the last line are variables A and B. The last two printf functions work the same. There are two occurrences of *pointer_1 and *pointer_2 in the program, please distinguish their different meanings.The "Pointer_1=&a" and "Pointer_2=&b" of the 5th and 6 lines of the program cannot be written as "*pointer_1=&a" and "*pointer_2=&b".

Dark Horse programmer--c Language Basic Grammar (ii)

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.