C Language Learning tutorial sixth chapter-pointers (2)

Source: Internet
Author: User
Tags define null integer printf

2. Operation of pointer variable

(1) Assignment operation

The assignment operation of a pointer variable has the following several forms:
① pointer variable initialization assignment, as described earlier.

② assigns the address of a variable to a pointer variable that points to the same data type. For example:
int A,*pa;
pa=&a; /* Assign the address of integer variable A to integer pointer variable pa*/

③ assigns the value of a pointer variable to another pointer variable that points to the same type of variable. Such as:
int a,*pa=&a,*pb;
PB=PA; /* Assign A's address to the pointer variable pb*/
Because PA,PB are all pointer variables that point to integer variables, they can be assigned to each other.

④ assigns the first address of an array to a pointer variable that points to an array.
For example: int a[5],*pa;
Pa=a; (The array name represents the first address of the array, so you can assign the pointer variable PA to the array)
Can also be written as:
pa=&a[0]; /* The address of the first element of the array is also the first address of the entire array.
can also give pa*/
Of course, you can also take the initialization assignment method:
int a[5],*pa=a;

⑤ assigns the first address of a string to a pointer variable of the character type. For example: Char *pc;pc= "C language", or written as: Char *pc= "C language" by initializing the assignment; Instead of loading the entire string into the pointer variable, the first address of the character array that holds the string is loaded into the pointer variable. It will be described in more detail later.

⑥ assigns the entry address of a function to a pointer variable that points to a function. For example: Int (*PF) ();p f=f; /*f is the name of the letter * *

(2) Addition and subtraction arithmetic operation

For pointer variables that point to an array, you can add or subtract an integer n. If the PA is a pointer variable to array A, then the PA+N,PA-N,PA++,++PA,PA--,--the PA operation is legal. The meaning of a pointer variable plus or minus an integer n is to move n positions forward or backward to the current position (point to an array element) that the pointer points to. It should be noted that the array pointer variable moves forward or backward one position and the address plus 1 or minus 1 is conceptually different. Because arrays can have different types, array elements of various types occupy a different length of bytes. such as a pointer variable plus 1, that is, move one position backward to indicate that the pointer variable points to the first address of the next data element. Instead of adding 1 on the basis of the original address.
For example:
int A[5],*pa;
Pa=a; /*pa points to array A and also points to a[0]*/
pa=pa+2; /*pa pointing to a[2], that is, the value of the PA is &pa[2]*/pointer variable addition and subtraction operation can only be the array pointer variable, the pointer to other types of variables to add subtraction operation is meaningless. (3) Operations between two pointer variables can only be performed between two pointer variables of the same array, otherwise the operation is meaningless.

Subtract ① two pointer variables
The difference between two pointer variables is the number of elements that differ between the index group elements of the two pointers. is actually the difference between two pointer values (addresses), divided by the length of the array element (the number of bytes). For example, PF1 and PF2 are two pointer variables that point to the same set of floating-point numbers, the value of PF1 is 2000H, and the floating-point number group is 4 bytes per element, so the PF1-PF2 result is (2000h-2010h)/4=4, representing PF1 and The difference between the PF2 is 4 elements. Two pointer variables cannot be additive. For example, what does PF1+PF2 mean? No real meaning.

② two pointer variables for relational operations
The relational operation of two pointer variables pointing to the same array can represent the relationship between their exponential group elements. For example:
PF1==PF2 indicates that PF1 and PF2 point to the same array element
PF1>PF2 indicates that PF1 is in a high address position
PF1<PF2 indicates that PF2 is in a low address position
Main () {
int A=10,B=20,S,T,*PA,*PB;
pa=&a;
pb=&b;
S=*PA+*PB;
T=*PA**PB;
printf ("a=%d\nb=%d\na+b=%d\na*b=%d\n", a,b,a+b,a*b);
printf ("s=%d\nt=%d\n", s,t);
}
......
Description PA,PB As Integer pointer variable
Assign a value to the pointer variable PA, the PA points to variable a.
To the pointer variable PB assignment, PB point to Variable B.
The meaning of the bank is to seek A+b's sum, (*pa is A,*PB is b).
The bank is the product of a*b.
Output results.
Output results.
......
The pointer variable can also be compared to 0. If p is a pointer variable, p==0 indicates that p is a null pointer and does not point to any variable; p!=0 indicates that p is not a null pointer. A null pointer is obtained by assigning a value of 0 to a pointer variable. For example: #define NULL 0 int *p=null; Assigning a value of 0 to a pointer variable is not the same as assigning a value. When a pointer variable is not assigned, it can be any value and cannot be used. Otherwise, an unexpected error will result. The pointer variable, when assigned a value of 0, can be used, except that it does not point to a specific variable.
Main () {
int a,b,c,*pmax,*pmin;
printf ("Input three numbers:\n");
scanf ("%d%d%d", &a,&b,&c);
if (a>b) {
pmax=&a;
Pmin=&b;}
else{
pmax=&b;
Pmin=&a;}
if (C>*pmax) pmax=&c;
if (c<*pmin) pmin=&c;
printf ("max=%d\nmin=%d\n", *pmax,*pmin);
}
......
Pmax,pmin is an integer pointer variable.
Enter a hint.
Enter three digits.
If the first number is greater than the second number ...
Pointer variable assignment
Pointer variable assignment

Pointer variable assignment
Pointer variable assignment
Judge and assign a value
Judge and assign a value
Output results
......

Related Article

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.