8, C language-pointer

Source: Internet
Author: User

Pointer

First, the pointer variable definition

   1, the definition of the format:

Type name * pointer variable name

The * Here is a flag, the indicator of the pointer variable
Pointer variable name is a user-compliant identifier
The first type after the semicolon is the definition statement, in addition to execute the statement

There are two kinds of variables in C language:

Variables (ordinary variables) store content values, constants are content values, can only be stored in ordinary variables;

Address variable (pointer variable) stores the address value;


Note:
1) Defining variables (normal variables, pointer variables) must be preceded by a type name
2) When defining a pointer variable, the "*" in front of the pointer variable name indicates that the current definition is a pointer type, just a flag. Variable. The asterisk is not a pointer variable name
3) The pointer variable is dedicated to the memory address, which prohibits assigning an integer value directly to a pointer variable


2. Reference to pointer variable
The "&" takes the address operator, and the address of the normal variable can be removed by the & operator
& Content variable = address value
The "*" pointer operator, * can take out the value of the normal variable pointed to by the pointer variable, (indirect reference to the normal amount)
* Three ways to use:
By
Sign
Pointer operators
* Address variable = content value
Pointer method: *p is placed on the left of the assignment number is a write operation, if placed on the right of the assignment number is read operation
Note:
1) You can assign a pointer variable "point to" a common variable (pointer variable =& normal variable)
2) The correct approach in C is to have the pointer variable point to a defined storage unit and then reference the storage unit to which it points by using the pointer variable
int *p;
*p = 200; This operation is dangerous, the pointer variable is defined and initialized before it can be used
3) the variable name (normal variable, pointer variable) represents the value within its storage unit
4) If the pointer variable p points to the variable A, the address of the variable A is assigned to the pointer variable p
int a=20,*p=&a;
A, *p<=>a
B, P<=>&a
C, &*p<=>&a<=>p
D, *&a<=>*p<=>a
E, (*p) + + a++
(*p)--a--
+ + (*p) ++a ++*p
--(*p)--a--*p

5) All pointer variables are assigned the same number of direct numbers in memory. sizeof ()

Description
1) int Fun (int. a[10]) <=>int Fun (int. *a) =int Fun (int a[])
& and * Mutual inverse
* equivalent to []
& and [] Mutual inversion
If the array is a formal parameter, the array name is treated as a pointer variable.
Second, pointer variable pointing to an array
1. Pointer variables to array elements
Because array elements are the same as normal, pointer variables that define pointers to array elements are predefined pointer variables that point to normal variables exactly as
Note:
1) in the C language: The array name represents the first address of the array, and is an address constant
2) When the pointer variable points to an element in the array, the pointer variable adds 1 to the next element of the array, and the pointer variable minus 1 o'clock points to the previous element in the array
2. Pointer variables pointing to one-dimensional arrays
int a[10];
int *p;
P=A;&LT;=&GT;P=&AMP;A[0]
Note:
1) in the C language: The array name represents the first address of the array, and is an address constant
2) When the pointer variable points to an element in the array, the pointer variable plus 1 points to the next element of the array, and the pointer variable minus 1 o'clock points to the previous element in the array
3) int a[n],*p=a;
P+i A+i &a[i]
* (p+i) * (a+i) a[i]<=>p[i]
When a pointer variable points to an array, the subscript operation ([]) is used for the array or after the pointer variable
* (p+i) * (a+i) a[i P[i]
p++ ++p p+=1 p=p+1
P----P p-=1 p=p-1
*p++ equivalent * (p++) The first address, take the old address content value, add the address down
*++p equivalent * (++p) First add the address down, the moved content value is removed
(*p) + + + + (*p) equivalent ++*p pointer does not move, but the pointer refers to the value of the content plus 1
(*p)--;--(*p) equivalent--*p

4) If the two pointer variables point to the same array, then the two pointer variables can be sized compared
5) The array in the formal parameter is actually a pointer variable, not a real array, because the value of the "array name" can be changed, and the value of the real array name cannot be changed
6) If the parameter is an array or pointer variable, the value of the argument can be changed in the function by the parameter
Third, pointer variables pointing to multidimensional arrays
1. Pointer variables pointing to multidimensional array elements
1) Only the column pointer is the "real" point to the element, that is, the storage unit that points to an element
2) A one-dimensional array name represents a column pointer, and a two-dimensional array name represents a row pointer

Note: If A is a two-dimensional array, then there are:
1) a+i is a row pointer, which is a whole line, and if you add 1 to it, point to the next line.
2) * (A+i) and A[i] is a column pointer that points to an element
3) * (a+i) +j and A[i]+j, both represent the address of the element A[i][j], which is equivalent to &a[i][j]. * (a+i) +j, A[i]+j, A[i][j]
4) * (* (a+i) +j), * (A[I]+J), (* (A+i)) [j] and A[i][j], all indicate element A[i][j]
Attention:
int w[2][3]
* (W+1) [2] is the element, calculated [] after the calculation (), and [] is applied to () ==w[3][0] The value is can be changed, and the value of the real array name cannot be changed
row [] column [] Element
Row * Column * element
2. Pointer variable to a one-dimensional array consisting of M elements
Defines the format of pointer variables that point to a one-dimensional array consisting of M elements:
Base type (* pointer variable name) [m];
int *p; column pointer variable
int (*p) [m] row pointer variable
element is a normal variable
One-dimensional array name is the first address, is the address constant, A=&a[0]

Iv. pointer variables pointing to strings
String constants: C pairs of character constants are treated as string constants at the first address
Char str[]= "ABCD" equivalent char str[]={"ABCD"}
Char *p= "ABCD" but not equivalent to char *p={"ABCD"}
Str= "ABCD" is illegal, constants cannot be placed to the left of the assignment number
p= "ABCD"

V. Pointer variables pointing to functions
The function name, like the array name, is the starting address and is an address constant

Defines the way a pointer variable is directed to a function:
Type name (* pointer variable name) ();

Small summary:
Type name (* pointer variable name) ();
Type name ordinary variable name;
Type an array group name [];
Type name * pointer variable name;
Type name Function name ()
{....}
Type name (* pointer variable name) [M];

Note:
1) When defining pointer variables that point to a function, be aware that there are two parentheses that must be there, and you do not need to define the parameters
2) A separate function name represents the first address of the function (the entry address of the function)
3) The pointer variable of the function can only point to the entrance of the function (first address of the function)
4) Assign a value to a pointer variable to a function, write only function names

Vi. functions that return pointers
Type name Function name (formal parameter)
{
}
The function that returns the pointer is defined in the following way:
Type name * Function name (formal parameter list)
{
}

Vii. pointer arrays and pointer variables pointing to pointers
1. If all elements of an array are pointer types (addresses), it is called an array of pointers

Format:
Type name * array name [constant expression]

Note: 1) Note the difference between it and a pointer variable that defines a one-dimensional array that is composed of M elements
Type name * array name [constant expression];
Type name (* pointer name) [constant expression m];
2) Each of its elements is a pointer type (address), that is, each of its elements is equivalent to a pointer variable
2. Pointer variables pointing to pointers
Pointer variables used to hold pointer variable addresses are called pointer variables
Define the format:
Base type name * * Pointer variable name

Eight, NULL pointer
Pointer variables can have null values, that is, pointer variables do not point to any variables, and do not point to any useful storage unit
NULL is defined as 0 in the system, that is, the value of NULL is 0
int A, B, C, *p=null;
At this point the value of P is a null pointer, that is, p does not point to any useful storage unit, although the value of NULL is 0, but we cannot assume that P points to a storage unit with address 0

Note: 1) When the value of a pointer variable is a null pointer, we cannot

8, C language-pointer

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.