1. Understanding Pointers
#include <stdio.h>//basic data type is passed as a function parameter is a value passed//void movefront (int x, int y)//{// x = x + 2;//}void Test () { / / determines the current coordinate int x =; int y =. printf ("%p\n", &x); printf ("%lu\n", &x); * ((int *) (0x7fff5fbff76c)) =; printf ("(%d,%d) \ n", x, y); Movefront (x, y); printf ("(%d,%d) \ n", x, y);} Suppose you want to access the pointer to the storage space, you must use the operator of the storage space pointed to by the Movefront (int *x, int *y) {// x = x + 2;//This is the point where the pointer is changed, Instead of visiting the storage space pointed to by the pointer *x = *x + 2;} int main (int argc, const char * argv[]) { // determines the current coordinate int x =; int y =. printf ("(%d,%d) \ n", x, y); Movefront (&x, &y); printf ("(%d,%d) \ n", x, y); return 0;}
2, the definition and initialization of pointers (key to grasp)
The smallest storage unit in memory: bytes. Each byte has a number in memory and this number is the pointer
Pointers: Memory Addresses
With the pointer, you'll have the memory key open and you can manipulate the memory.
Pointer variable: The variable that holds the memory address
Define pointer: Pointer to the data type * pointer variable name;
When defining a variable, * is a type specifier that defines that the variable is a pointer variable
At the time of the variable that is not defined. * is a operator. Access (read, write) The block of storage that the pointer points to is empty
Initialization of the pointer:
Note the point:
1, there is only a definition of no initialization pointer inside is a garbage value. This time we became the pointer for the wild pointer
2, assume the operation of a wild pointer
2.1 Program crashes
2.2 Visitors should not be asked to access the store. Manipulating Potential logic errors
3. Cannot assign a pointer variable with an integer constant
Because the memory is assigned to us by the operating system, it's not our random pick.
4, what type of pointer. Just what type of variable to point to
5. Multiple pointers can point to the same variable
6, the pointer is able to change the direction of
#include the definition of the <stdio.h>//pointer Void Test () { int num = ten; defines a pointer variable int *p; p = # *p =; printf ("num =%d\n", num);} int main (int argc, const char * argv[]) {//First defined in initialization int num = 10;// defines a pointer variable p int * p; *p = #//p also has the initial. You can not access it point to storage space P = #//p points to num *p =; defines the pointer variable at the same time as the initial int num2 =; int *p2 = &num2; *P2 = +; printf ("%d,%d\n", NUM2,*P2); You cannot assign a pointer variable with an integer constant//Because memory is assigned to us by the operating system, not our random / int *p3 = 100000;//Here is the wrong/// / *p3 = ten; P2 = # printf ("%p\n", p2); char c = ' a '; int *pc = &c; *PC = ten; printf ("%p\n", p2); return 0;}
3. Multi-level pointer
A pointer to a variable is called an indirect interview.
Because pointer variables point directly to variables, they are called "first-level pointers." and
It is assumed that a pointer variable that points to a pointer will constitute a "level two pointer".
#include <stdio.h>void Test () { int num = ten; int *p = # // Define a pointer to point to the variable p // PP is a level two pointer int **pp = &p; **PP = +; printf ("%d\n", num); int ***PPP = &pp; PPP = +; printf ("%d\n", num); level four pointer int ****pppp = &ppp; PPPP = +; printf ("%d\n", num); } void ReadFile (char **error) { *error = "Read error"; } int main (int argc, const char * argv[]) { // char error[100]; char *error; ReadFile (&error); printf ("%s", error); return 0;}
4, why the pointer to distinguish the type
1. The address of the variable is the first address of the storage space where the variable resides.
2, the pointer variable can only store an address number, assuming there is no type. You don't know how many bytes of storage to access when you pass a pointer
3. The pointer distinguishes the type so that it can access the storage space pointed to by the pointer.
4. Assume that a variable of int is manipulated by a pointer of type char. If the binary data of the value is more than 1 bytes, then the data error is caused
5. If you manipulate a char variable by a pointer of type int, you will change the memory you should not change, resulting in a program logic error
#include <stdio.h>/* all pointer types are storage spaces that occupy eight bytes */void testeverypointeris8b () { printf ("%lu\n", sizeof ( int *)); printf ("%lu\n", sizeof (char *)); printf ("%lu\n", sizeof (double *)); printf ("%lu\n", sizeof (float *)); printf ("%lu\n", sizeof (float *));} int main (int argc, const char * argv[]) { int num = ten; char *CP = # printf ("%d\n", num); return 0;}
5. Overview of pointer operations
Pointer variable: The address number in which the memory byte is stored (unsigned number of shaping)
Pointer: is an unsigned number of operations constrained
Arithmetic operation:
Pointer + shape Number = = = Pointer variable median value + sizeof (the data type it points to)
Pointer-integer Number = = = Pointer variable median-sizeof (the data type it points to)
Pointer1-pointer2 = (pointer1 median-pointer2 median)/sizeof (which points to the data type)
Assignment operation:
=
+ = must be an integer number
-= must be an integer number
Comparison operation
==
!=
>
<
>=
<=
Self-increment self-reduction
p++; p = p + 1;
++p; p = p + 1;
--p;
p--;
#include <stdio.h>//arithmetic operation void Test () { int a = ten; int *p = &a; pointer +1 p = p + 1; int nums[5] = {1,2,3,4,5}; int * Pointer1 = nums; int * Pointer2 = &nums[4]; size_t size = pointer2-pointer1; printf ("%lu\n", size); pointer1 + pointer2; pointer2 * POINTER1; Pointer1/pointer2; POINTER1/2;} Assignment operation void Test1 () { int a = ten; int *p = &a; int nums[] = {1,2,3,4,5}; int *p = nums; int *p2 = nums; p + = 2; p = p + 2; P-= 1; printf ("%d\n", *p); } Relational operation int main (int argc, const char * argv[]) { int nums[] = {1,2,3,4,5}; int *p = nums; p++; int result = Nums = = p; result = p > nums; p--; result = P < nums; result = P >= nums; result = P <= nums; printf ("%d\n", result); return 0;}
6. Pointers and one-dimensional arrays (understanding)
An array is like a pointer: access to an array of elements, using an array and using pointers to this array is equivalent
NUMS[1] = = P[1]
nums+1 = = = p + 1;
The nature of Nums[1] * (nums + 1)
Pointer + integer ===== value in pointer + sizeof (data type pointed to) * integer
int nums[] = {1,2,3,4,5};
//
int *p = Nums;
Double nums[] = {1.0,2.0,3,4,5};
Double * p = nums;
printf ("%d,%d,%d,%d,%d,%d\n", nums[1],p[1],* (Nums + 1), * (P + 1), * (++p),.
);
printf ("%p\n", nums);
printf ("%p\n", nums+2);
printf ("%p\n", p);
printf ("%p\n", p+2);
Array is not a pointer
1. sizeof (array)! = sizeof (pointer): When an array is assigned a pointer variable, then some information in the array is lost, such as the length of the array, such that the pointer information is missing
2, pointers can be changed, the direction of the array is not able to change
3, Array = = &array Array name is the array address, pointer! = &pointer: The pointer is pointing to the address is not the pointer itself address
#include <stdio.h>int main (int argc, const char * argv[]) { int nums[] = {1,2,3,4,5}; int *p = nums; p = nums;// nums = nums + 1; printf ("%lu,%lu\n", sizeof (Nums), sizeof (p)); printf ("%p\n", nums); printf ("%p\n", &nums); printf ("%p\n", p); printf ("%p\n", &p); return 0;}
7. Pointers and two-dimensional arrays
The difference between a pointer array and a two-dimensional array pointer variable
You should pay attention to the differences between pointer arrays and two-dimensional array pointer variables. Both of these can be used to represent two-dimensional arrays, but their representation and meaning are
Different.
A two-dimensional array pointer variable is a single variable, in its general form "(* pointer variable name)" On both sides of the parentheses are not few. And the pointer array type represents the
is multiple pointers (a set of ordered pointers) in the general form "* Pointer array name" cannot have parentheses on either side.
Like what:
int (*p) [3];
Represents a pointer variable that points to a two-dimensional array. The number of columns in the two-dimensional array is 3 or the length of the decomposition into a one-dimensional array is 3.
int *p[3]
Indicates that P is an array of pointers with three subscript variables p[0]. P[1],P[2] are pointer variables.
#include <stdio.h>void test () {int nums[3][2] = {{1,2},{3,4},{5,6}}; int *p = nums[0]; printf ("%p\n", p); printf ("%p\n", nums); for (int i = 1; i < 6; i++) {printf ("%d", * (P + i)); }/* defines the format of the pointer array: data type * pointer variable name [number of pointers] */void test2 () {int nums[3][2] = {{1,2},{3,4},{5,6}}; int * P[2] = {nums[0],nums[1]}; p = nums; printf ("%d\n", p[0][1]); int a = 10; int B = 20; int c = 30; int *p = &a; *p = = = P[1]; Not so written int *ps[3] = {&a,&b,&c}; printf ("%d,%d,%d", *ps[0],*ps[1],*ps[2]); }/* defines a pointer data type that points to a one-dimensional array (* pointer name) [the number of elements of a one-dimensional array to point] pointer + integer = = value in the pointer + the length of the data type that is pointed to * integer */int main (int argc, const char * AR Gv[]) {int nums[3][2] = {{1,2},{3,4},{5,6}}; int (*PS) [2]; PS = nums;//can feel that PS and nums are equivalent int num = ps[0][1]; printf ("%d\n", num); printf ("%p\n", nums); printf ("%p\n", nums+1); printf ("%p\n", PS); PriNTF ("%p\n", ps+1); for (int i =0; i < 3; i++) {for (int j = 0; J < 2; J + +) {printf ("%d", ps[i][j]); } printf ("\ n"); }//Nums nums[0]//Same point: the corresponding address is the same//different points: pointer type is different//nums + 1 = nums + sizeof (Nums[0])//nums[0] + 1 = nums + sizeof (int)//sizeof (nums) two-dimensional array used to occupy storage space bytes//sizeof (Nums)/sizeof (int) Two-dimensional array in a common number of int data int *p = Nums[0]; for (int i = 0; i < sizeof (nums)/sizeof (int); i++) {printf ("%d", p[i]); } return 0;}
iOS Learning Journey---pointers are not difficult