C Language Basics Tutorial (iv) Pointers, structures, unions, and enumerations (2)

Source: Internet
Author: User
1.1.2 A reference to a pointer variable
Since you can only store addresses in pointer variables, do not assign an integer to a pointer variable in use. The following assignment is not valid:
int *ip;
ip=100;
Assume
int i=200, x;
int *ip;
We have defined two integer variables I, x, and also defined a pointer variable IP that points to an integer number. I, x can hold an integer, and IP can only hold the address of an integer variable. We can assign the address of I to IP:
ip=&i;
At this point, the pointer variable IP point to integer variable i, assuming that the variable I address is 1800, this assignment can be image as shown in the following diagram of the link.
IP I
┏━━━┓┏━━━┓
┃1800╂──→┃200┃
┗━━━┛┗━━━┛
Figure 1. Assign a value to a pointer variable
We can then indirectly access the variable i via pointer variable IP, for example:
X=*ip;
operator * Accesses the storage area with IP as the address, and the IP store is the address of the variable I, therefore, *IP access to the address of the storage area of 1800 (because is an integer, is actually starting from 1800 two bytes), it is the storage area occupied by I, so the above assignment expression is equivalent to x=i;
In addition, the pointer variable is the same as the general variable, and the values stored in them can be changed, that is to say, to change their direction, assuming
int I, J, *p1, *P2;
I= ' a ';
J= ' B ';
p1=&i;
p2=&j;
The links shown in the following illustration are established:
P1 I
┏━━━┓┏━━━┓
┃╂──→┃ ' A ' ┃
┗━━━┛┗━━━┛
P2 I
┏━━━┓┏━━━┓
┃╂──→┃ ' B ' ┃
┗━━━┛┗━━━┛
Figure 2. Assignment operation result
Then the assignment expression:
P2=p1
So that P2 and P1 point to the same object I, then *P2 is equivalent to I, not J, figure 2. This becomes Figure 3.
P1 I
┏━━━┓┏━━━┓
┃╂──→┃ ' A ' ┃
┗━━━┛┌→┗━━━┛
P2│j
┏━━━┓│┏━━━┓
┃╂─┘┃ ' B ' ┃
┗━━━┛┗━━━┛
Figure 3. The situation when P2=P1
If you execute the following expression:
*P2=*P1;
is to assign the P1 to the area referred to by P2, which is shown in Figure 2. It becomes Figure 4.
P1 I
┏━━━┓┏━━━┓
┃╂──→┃ ' A ' ┃
┗━━━┛┗━━━┛
P2 J
┏━━━┓┏━━━┓
┃╂──→┃ ' A ' ┃
┗━━━┛┗━━━┛
Figure 4. The situation when *P2=*P1
Accessing a variable that it points to through a pointer is an indirect access, so it takes time and is not intuitive to access a variable directly, because the pointer to which variable depends, depending on the value of the pointer (that is, pointing to), such as "*P2=*P1;" is actually "J=i", the former is not only slow but also unclear. But because pointers are variables, we can indirectly access different variables by changing their point of view, which gives programmers flexibility and makes program code more concise and efficient.
The pointer variable can appear in an expression, set int x, y *px=&x;
The pointer variable px points to an integer x, then *px can appear anywhere x can appear. For example:
y=*px+5; /* means to add 5 to the contents of X and assign it to y*/
Y=++*PX; /*PX content plus 1 to assign to Y [++*px equivalent to + + (px)]*/
y=*px++; /* equivalent to Y=*PX; px++*/

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.