Pointer_ one
1, Basic concept of pointer
Computer memory is addressed in the smallest unit of bytes.
1. The address is the number of the memory unit.
2. The pointer is the address, and the address is the pointer.
3. Pointer variables are variables that store addresses (that is, pointers). Pointer and pointer variable are two different concepts.
4. However, it should be noted that pointer variables are usually referred to as pointers when we describe them. In fact, they have different meanings.
int i; int *p; //p is the name of the variable, and the data type of the p variable is int *. Int * indicates that the p variable stores the address of the int variable. p = &i; //ok /* 1,p The address of i is saved, so p points to i. 2,p Not i, i is not p. Modifying the value of i does not affect the value of p, and modifying the value of p does not affect the value of i. 3,If a pointer variable points to an ordinary variable (that is, the address where the pointer variable stores the ordinary variable), then *Pointer variable It's exactly the same as Common variable 4,*p It is a variable stored in space with the content of p as the address */ p = i; //error, because the types are inconsistent, p can only store the address of int type variables, not int type values.
2, Importance of pointers
1. It represents some complex data structures
2. Fast data transmission
3. Make the function return more than one data
void f(int *pa, int *pb) { *pa = 1; *pb = 2; //The called function modifies the value of the main calling function -- the pointer can do it, but others can't } int main(void) { int a = 6, b= 8; f(&a, &b); }
4. Direct access to hardware
5. It can easily process strings
6. It is the basis for understanding object-oriented language references
3, Definition of pointer
Address:
Memory unit number
Non negative positive number starting from zero
Range: the maximum access space of 32 bits is 4G
Pointer:
1. The essence of the pointer is a non negative integer with limited operation (the pointer can be subtracted, but cannot be added, multiplied or divided)
2. The pointer stores the address, which is a non negative integer of 4 bytes (32-bit machine). No matter how large the data type is, the pointer only retains the address of the first byte of the data (the data type determines the size of bytes occupied by each data in memory), so any type of pointer is 4 bytes in size.
4, Classification of pointers
1. Basic type pointer
Common errors with pointers
1.1 field pointer
int main(void) { int *p; //Local variables are uninitialized and store garbage values int i = 5; *p = i; //Assign the value of i to a memory with the garbage value as the address. In this program, the system only allocates 2 numbers of empty operations Space (p, i), but the space other than the allocated space is operated during the operation of the program. printf("%d\n", *p); return 0; }
int main(void) { int i = 5; int *p, *q; p = &i; *q = p; //If q is an int * type, then * q is an int type and p is an int * type. The types are inconsistent and cannot be assigned. A syntax error occurs return 0; }
/* q The space of belongs to this program, so this program can read and write the content of Q. however, if q is a garbage value, this program cannot read and write the content of * q (that is, the content in the space with Q content as the address), because the control authority of the memory unit represented by * q is not allocated to this program. */ int main(void) { int i = 5; int *p, *q; p = &i; p = q; printf("%d\n", *p); //error: because q itself allocates space (but the value of p is the garbage value in the q local variable), But * p is the content of a garbage value address (exceeding the space allocated by the system). printf("%d\n", *q); //error: Operation q itself is OK, but operation * p is out of bounds. return 0; }
1.2 use of basic type pointer
Swap 2 numbers
#include <stdio.h> void Huhuan(int *pa, int *pb) { int c; c = *pa; *pa = *pb; *pb = c; } int main(void) { int a = 3; int b = 4; printf("*pa = %d, *pb = %d\n", a, b); printf("///\n"); Huhuan(&a, &b); printf("*pa = %d, *pb = %d\n", a, b); return 0; }
1.3 meaning of * sign
1,multiplication 2,Define pointer variables int *p; Defines a name that is p Variable for, int* *express p Can only store int Address of the type variable. 3,Pointer operators The operator is placed in front of the defined pointer variable; If p Is a defined pointer variable, then*p Is to p The content is the variable stored in the address space.
1.4. How to modify the value of a common variable in a calling function through a called function
1,The argument must be the address of the ordinary variable 2,The formal parameter must be a pointer variable of this type. 3,Pass in the called function *Formal parameter name = ...... You can modify the values of the variables related to the main function.
2. Pointers and arrays
2.1 pointers and one-dimensional arrays
One dimensional array name:
The one-dimensional array name is a pointer constant, which stores the address of the first element of the one-dimensional array.
//error int a[5]; b[3]; a = b; //error, the one-dimensional array name is a pointer constant
#include <stdio.h> int main(void) { int a[3]; printf("%#X\n", &a[0]); printf("%#X\n", a); } //Operation results fuchunjie@FCJ:/mnt/hgfs/VMshare/mySpace/Pointer exchange 2 number $ ./app 0XD5E2C42C 0XD5E2C42C //Proof: the name of one-dimensional array is a pointer constant, which stores the address of the first element of one-dimensional array.
Relationship between subscript and pointer
If p Is a pointer variable, then p[i] Always equivalent to *(p+i);
How to determine a one-dimensional array (several parameters are required to determine a one-dimensional array)