1. The concept of pointers
In the computer, all the data is stored in the memory. In general, a byte in memory is called a memory unit, different data types occupy a number of memory units, such as the integer 2 units, the character of 1 units, etc., in the second chapter has been described in detail. In order to access these memory cells correctly, each memory unit must be numbered. The internal memory element can be found accurately based on the number of a single cell. The number of the memory unit is also called the address. Since the memory unit can be found according to the number or address of the memory unit, it is often referred to as a pointer. The contents of a memory unit's pointer and memory unit are two different concepts.
2. The pointer is a variable
A. The pointer is a variable, the pointer can be stored value, the value is the address value of another variable (memory address value)
B. The system allocates memory space for the pointer
C. The pointer has its own address
D. The pointer is like any other variable or constant, and the pointer must be declared before it can be used.
E. Use the hyphen (&) to get the address value:
3. Type of pointer and type to which the pointer is pointing
A. Type of pointer
From a grammatical point of view, just remove the pointer name from the pointer declaration, and the remainder is the type of the pointer, which is the type of the pointer itself.
For example: (1) int *ptr; The type of pointer is int *
(2) Char *ptr; The type of pointer is char *
(3) int **ptr; The type of pointer is int * *
(4) int (*PTR) [3]; The type of pointer is int (*) [3]
(5) int * (*PTR) [4]; The type of pointer is int * (*) [4]
B. The type that the pointer points to
When you use a pointer to access the memory area pointed to by the pointer, the type that the pointer points to determines what the compiler will look for in that memory area.
Syntactically, only the pointer declaration in the statement of the name of the pointer and the left side of the pointer declarator "*" is removed, leaving the pointer to the type.
The general form of the pointer is: type specifier * variable name;
where * indicates that this is a pointer variable, the variable name is the defined pointer variable name, and the type specifier represents the data type of the variable to which this pointer variable is pointing.
For example: int *p1; indicates that p1 is a pointer variable whose value is the address of an integer variable. Or, P1 points to an integer variable. As to which integer variable the P1 points to, it should be determined by the address assigned to P1.
4. Relation operation of pointer variables
Two pointer variables that point to the same array are relational operations that represent the relationships between the elements of their exponential group. 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 location
PF1<PF2 indicates that PF2 is in a low address location
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 is an integer pointer variable
Assign the pointer variable PA to the variable A.
The pointer variable PB is assigned, PB points to the variable B.
The meaning of the bank is to seek the sum of A+b, (*pa is A,*PB is b).
The bank is seeking the a*b of the product.
Output results.
Output results.
......
Pointer variables can also be compared with 0. Set p as the pointer variable, then p==0 indicates that p is a null pointer, which 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 a value, it can be any value and cannot be used. Failure to do so will cause an unexpected error. When the pointer variable is assigned a value of 0, it 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 a three number.
If the first number is greater than the second number ...
Pointer variable assignment
Pointer variable assignment
Pointer variable assignment
Pointer variable assignment
Judging and assigning values
Judging and assigning values
Output results
5. The difference between a string pointer variable and a character array
Both character arrays and character pointer variables enable the storage and operation of strings. But there is a difference between the two. There are several issues to be aware of when using:
A. The string pointer variable itself is a variable that holds the first address of the string. The string itself is stored in a contiguous memory space headed by the first address and ends with '/0 ' as a string. Character arrays are composed of a number of array elements that can be used to hold the entire string.
B. To initialize a character array, an external type or static type must be used, such as static char st[]={"C Language"}, and no such restriction on string pointer variables, such as Char *ps= "C Language";
C. The string pointer way char *ps= "C Language"; can be written as: Char *ps; ps= "C Language", while the array method:
static char st[]={"C Language"};
cannot be written as:
Char st[20];st={"C Language"};
Instead, you can assign values to individual elements of a character array.
From the above points we can see the difference between the string pointer variable and the character array when used, but also can see that the use of pointer variables more convenient. As mentioned earlier, it is dangerous to use a pointer variable before it gets a definite address, which can cause errors. However, it is possible to assign values directly to pointer variables. Because the C system assigns a value to the pointer variable, it is given a definite address. So
Char *ps= "C langage";
or char *ps;
ps= "C Language"; it's all legal.
6. Constant pointer and pointer constants
A. Constant pointers
A constant pointer is a pointer to a constant, and the contents of the memory address pointed to by the pointer are non-modifiable.
Constant pointer defines "const INT *p=&a;" Tells the compiler that *p is a constant and cannot manipulate *p as an lvalue. But the pointer p here is also a variable whose contents hold the address of the constant, so it is permissible to declare the constant pointer again, and the pointer is allowed to be modified, for example:
int a=0,b=1;
const int *p; Declaring a constant pointer p
p=&a; P Point to a
p=&b; Modify the pointer p to point to B, allowing
*p=2; Not allowed
B. Pointer constants
A pointer constant is a constant of a pointer, which is a pointer to an immutable address, but can be modified by what it points to.
Pointer constants define "int *const p=&a;" Tells the compiler that p is a constant and cannot operate as an lvalue, but allows you to modify what it points to, that is, *p is modifiable. A pointer constant must be initialized at the same time as it is declared, and it is not allowed to declare a pointer constant before assigning it, which is the same as declaring a general constant, for example:
int a=0,b=1;
int *const p1=&a;
int *const P2; Not allowed, it must be initialized
p2=&b; Not allowed, p2 is a constant is not allowed as a left value
*p1=2; Allows you to modify the value of the pointer *p1
6. On a machine that uses a nonzero null pointer to internally Express, how NULL defines
As with other machines: defined as 0 (or some form of 0). When a programmer requests a null pointer, the compiler generates a binary representation that is appropriate for the machine, regardless of whether it writes "0" or "NULL". Therefore, defining NULL as 0 on a machine with a null pointer that is not 0 is as legal as any other machine: the unmodified 0 that the compiler sees in the pointer context produces the correct null pointer.
7. If null and 0 are equivalent to null pointer constants, which one should be used.
Many programmers think that the pointer context should use NULL to indicate that the value should be considered a pointer. Others think that defining 0 with a macro will only complicate things, and they tend to use unmodified 0.
C programmers should understand that NULL and 0 are completely equivalent in the context of pointers, while unmodified 0 can be fully accepted. Any place where null is used should be seen as a gentle pointer hint, but programmers cannot rely on it to differentiate between pointers 0 and integers 0.
"Best practices" although null and 0 have the same functionality, it is recommended to use NULL instead of 0. There are two benefits of this practice:
A. You can assume that the value of NULL has changed, for example, that using NULL on a machine that uses a non-0 internal null pointer is better than 0, but that is not the case.
B. Although symbolic constants often replace numbers to prepare for changes in numbers, this is not a null substitution for 0. The language itself ensures that 0 (for pointer context) in the source code generates a null pointer. Null is used only as a formatting habit.
8. Universal pointer void
A void pointer is generally referred to as a generic pointer or a generic pointer, which is a C language Convention on "Pure address." The void pointer points to an object, but the object does not belong to any type. Take a look at the example below.
int * IP;
void *p;
In the example above, IP points to an integer value, and P points to an object that does not belong to any type.
In the C language, you can use other types of pointers at any time instead of void pointers, or use void pointers instead of other types of pointers, and you do not need to cast them. For example, you can pass a pointer of type char* to a function that requires a void pointer.
A void pointer can be used when pure memory operations are performed, or when a pointer to an undetermined type is passed. Void pointers are also often used as function pointers.
Some C code is purely memory-operated. In earlier versions of the C language program, this was implemented by the character pointer "char*", but this was confusing because it was not easy to determine whether a character pointer would point to a string, a character array, or simply point to an address in memory.
For example, the STRCPYO function copies a string to another string, and the Slcpyo function copies part of one string into another string.
Char *strcpy (char ' strl,const char *str2);
Char *strncpy (char *strl, const CHAR*STR2, siz.e_t N);
The MEMCPYO function copies the in-memory data from one location to another.
void *memcpy (void *addrl, void *addr2,size_t N);
The memcpy () function uses a void pointer to indicate that the function is purely memory-copied, and any content, including the null character (0 bytes), is copied. Take a look at the example below.
#include "thingie, h"/*defines struct thingie */
struct Thingie * p_src * p_dest;
/ *... * /
Memcpy{p_dest, p_src, sizeof (struct thingie) * numthingies);
In the example above, the memcpy () function replicates an object that is stored in a struct thingie struct op_dest
And P_SRC are pointers to the struct thingie struct, the memcpy () function copies the contents of "sizeof (stuct thingie) *numthingies" bytes starting at the point where the p_src points to from P_ Dest points to the beginning of a piece of memory in the area. For the memcpy () function, both p_dest and p_src are simply pointers to an address in memory.
9. Pointer arrays and Arrays pointers
Pointer array: An array of pointers can be said to be "an array of pointers", first this variable is an array, and secondly, the "pointer" modifies the array, meaning that all elements of this array are pointer types, and in 32-bit systems, the pointer occupies four bytes.
Array pointer: An array pointer can be said to be "pointer to an array", first the variable is a pointer, and secondly, the "array" modifies the pointer, meaning that the pointer holds the first address of an array, or that the pointer points to the first address of an array.
Array pointers (also called row pointers)
define INT (*p) [n];
() high priority, the first explanation is that p is a pointer to an integer one-dimensional array, the length of the one-dimensional array is n, it can be said that the step of P. In other words, when executing p+1, p crosses the length of n integer data.
To assign a two-dimensional array to a pointer, you should assign this value:
int a[3][4];
int (*p) [4]; The statement defines an array pointer to a one-dimensional array with 4 elements.
P=a; Assign the first address of the two-dimensional array to p, i.e. a[0] or &a[0][0]
p++; After the statement executes, that is, P=p+1;p crosses line a[0][] points to the line a[1][]
So an array pointer is also called a pointer to a one-dimensional array, also known as a row pointer.
Array of pointers
define int *p[n];
[] High priority, the first combination with P as an array, and then by int* that this is an integer pointer array, it has n pointer type array elements. The execution of p+1 here is wrong, so the assignment is also wrong: P=a, because P is an unknown representation, only p[0], p[1], p[2]...p[n-1], and they are pointer variables that can be used to hold variable addresses. But can be so *p=a; Here *p represents the value of the first element of the pointer array, the value of the first address of a.
To assign a two-dimensional array to a pointer array:
int *p[3];
int a[3][4];
for (i=0;i<3;i++)
P[i]=a[i];
Here int *p[3] indicates that a one-dimensional array holds three pointer variables, respectively p[0], p[1], p[2], so assign values separately.
The array pointer is just a pointer variable, which appears to be a C language specifically designed to point to a two-dimensional array, which occupies the memory of a pointer to the storage space. Pointer arrays are multiple pointer variables that exist in the form of an array of memory, occupying multiple pointers of storage space. It is also important to note that both the reference and the array name reference are the same when used to point to a two-dimensional array.
10. Pointer functions and function pointers
A. function pointer
function pointer is a pointer variable that points to a function, so the function pointer itself should first be a pointer variable, except that the pointer variable points to the function. This is just like using pointer variables to point to integer variables, character types, and arrays, and here is a pointer to a function. As mentioned earlier, C at compile time, each function has an entry address, which is the address pointed to by the function pointer. Once you have a pointer variable to a function, you can call the function with that pointer variable, just as you can refer to other types of variables using pointer variables, which are conceptually consistent. A function pointer has two uses: calling a function and making a function argument. The function pointer is described as:
Data type marker (* pointer variable name) (parameter);//The arguments in the function brackets are optional, depending on the situation.
If we have an int test (int a) function, then its address is the name of the function, like an array, the name of the array is the starting address of the array. Define a pointer to a function in the following form, for example:
Int (*FP) (int a), //This defines a pointer to a function
fp= Test //assigns the address of the function test to the function pointer fp
typedef definitions can simplify the definition of a function pointer, not feel when defining a time, but the definition is more convenient, the above code is rewritten as follows:
typedef int (*FP) (int a),
//This is not a life function pointer, Instead, define the type of a function pointer, which is defined by itself, and the type is named FP
&NBSP;FP FPI; &N Bsp //Here you define a FPI function pointer using your own defined type name FP!
fpi=test;
The
B. Pointer function
Pointer function refers to a function with a pointer, which is essentially a function. We know that a function has a return type (or no value if it does not return a value), except that the pointer function return type is a pointer to a type. The definition format is as follows:
return type identifier * return name (formal parameter table)
{
function body
}
Return type can be any base type and composite type. The
function that returns a pointer is very versatile. In fact, every function, even if it does not carry a pointer that returns a certain type, itself has an entry address that is equivalent to a pointer. For example, the function returns an integer value, which is actually equivalent to returning the value of a pointer variable, but the variable is the function itself, and the entire function is equivalent to a "variable".
Example:
float *find (float pointer[],int N) /* define pointer function */
{
Float *pt; ,
pt=* (pointer+n);
Return (PT);
} < br>