Pointer advanced (I)
Pointer operation
1. Pointer operation
Pointer + integer = pointer
Pointer integer = the rules for pointer and addition are the same.
Conclusion: actual increase \ decrease = integer + - the memory size of the type pointed to by the pointer
int a = 10,b = 20,c = 30; printf("a=%d,b=%d,c=%d\n",a,b,c); int *pa = &a,*pb = &b,*pc = &c; printf("pa=%d,pb=%d,pc=%d\n",pa,pb,pc); int *pd = pa +10; printf("pd = %d\n",pd); double d = 45.6; double *pdouble = &d; printf("pdouble = %d\n",pdouble); printf("pdouble + 5 = %d\n",pdouble + 5); char ch = 'A'; char *pch = &ch; printf("pd = %d\n",pch); printf("pd = %d\n",pch + 5);
Pointer - pointer = unit length
printf("%d\n",pa - pb );
Pointer operation
Used in a continuous memory space, array, dynamically opened memory
int arr[10] = {1,2,3,4,5,6,7,8,9,10}; printf("arr = %d\n",arr); printf("arr = %d\n",&arr[0]); printf("arr = %d\n",&arr[1]); //arr is a constant //The type of arr points to a pointer of type int int* parr = arr; printf("arr = %d\n",arr+1); printf("arr = %d\n",arr+2); printf("arr = %d\n",arr+10);//Can print, but it's out of bounds
Relationship between pointer and one-dimensional array:
//deduction arr + 0 = &arr[0] arr + 1 = &arr[1] arr + 2 = &arr[2] ... arr + 3 = &arr[3] //Continue derivation *(arr + 0) = arr[0] *(arr + 1) = arr[1] *(arr + 2) = arr[2] ... *(arr + 3) = arr[3]
be careful:
, < the size of these two judgment pointers needs to be compared in a continuous memory space.
== != The two can be compared at will.
Pointer advanced (II)
Relationship between pointer and two-dimensional array:
1. What does arr mean? What is the first address type? Not a pointer to an int, but a pointer to a one-dimensional array
2. You can make a two-dimensional array into a one-dimensional array, but each element in this one-dimensional array has five elements
int i = 0; int j = 0; int arr[4][5]= { {1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15}, {16,17,18,19,20} }; for(i = 0;i<4;i++) { for(j = 0;j<5;j++) { printf("a[%d][%d] = %d ",i,j,arr[i][j]); //Another access method: printf("a[%d][%d] = %d ",i,j,*(*(arr+i) + j)); } // printf("\n"); }
Pointer array: it is an array. All stored in the array are pointers.
int * arr[4] = {&a,&b,&c,null };
Array pointer: is a pointer
typedef int(*PARR)[5];//Is a new type, array pointer type arr; //The array pointer points to an array int (*parr)[5] = arr;//The array pointer is now defined PARR parr2 = arr;
How much is the address offset?
printf("%d\n",arr + 0);//Bytes offset = type * Size printf("%d\n",arr + 1); printf("%d\n",arr + 2); printf("%d\n",arr + 3); printf("\n");
Indirect access
printf("%d\n",*(arr + 0));//Get the whole array, array name = "arr2[0] printf("%d\n",*(arr + 1)); printf("%d\n",*(arr + 2)); printf("%d\n",*(arr + 3));
deduction:
*(arr + 0) = >arr[0] *(arr + 1) = >arr[1] *(arr + 2) = >arr[2] *(arr + 3) = >arr[3]
arr + 0; // Type is a pointer to the entire one-dimensional array
arr[0]; // A type is a pointer to an integer
*(*(arr+0) + 0) => arr[0] + 0; *(arr+0) + 2 =>&arr[0][2]=> arr[0] + 2; *(*(arr+0) + 2) =>3//Get the number 3 printf("%d\n",*(*(arr+1) + 2));
deduction
*(*(arr+0)+0) => arr[0][0] *(*(arr+0)+1) => arr[0][1] *(*(arr+0)+2) => arr[0][2] *(*(arr+0)+3) => arr[0][3]
3D array:
int arr[2][3][4] = {0,1,2,3}; int (*parr)[3][4] = arr;
The relationship between pointer and string. The type of this pointer is char *
//First address of string, pointer printf("%d\n","hello,world"); char *str = "hello world"; printf("%s\n",str);
*"hello,world"; printf("hello,world = %c\n",*"hello,world"); //What you get is h printf("hello,world = %c\n",*("hello,world"+1)); //Print out e
Array subscript access and array indirect access
printf("hello,world [0]= %c\n","hello,world"[0]);
Array pointer: a pointer to an array
The difference is that the occupied space is different.
char str[4][10] = {"hello world","china","123456","yellow"}; char *strarr[4] = {"hello world","china","123456","yellow"}; for(i = 0;i<4;i++) { printf("%s\n",strarr[i]); }
Advanced pointer (III)
Exchange the values of two variables
The value and the address are different. The value is a copy of the pass. Whether the value is passed or the address is passed, it is a copy.
void swap(int *pa,int *pb) { int temp = *pa; *pa = *pb; *pb = temp; }
void display(char* str) { printf("%s\n",str); }
Pass an array
When an array is passed as a function parameter, the first address is passed instead of the entire array. It has become an address instead of an array.
//Mode 1 void printArr(int arr[],int length); //Mode 2 void printArr(int *arr,int length) { for(int i; i<length;i++) { //printf("%d, ",arr[i]); printf("%d, ",*(arr+i)); } printf("\n"); } //Main function printArr(arr,sizeof(arr)/sizeof(arr[0]));
A two-dimensional array is passed as a function parameter.
void printArr2(int (*arr)[5],int x,int y) { for(int i = 0; i<x;i++){ for(int j = 0; j<y;j++) { printf("%d, ",arr[i][j]); } printf("\n"); } } //Two dimensional arrays are passed as function parameters int arr2[4][5] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; printArr2(arr2,4,5);
void showdouble(double *d) { printf("Floating point number:%f\n",*d); } double d = 456.678; showdouble(&d);
Pointer as the return value of the function
Note: the returned address must be available, that is, it is not released.
int* test() { int a= 10,b=20,c=10; c = a + b; return &c; } int *p = test(); *p = 45; //It cannot be used later printf("p=%d\n",*p);
//No problem int* test1(int *c) { *c = 1000; return c; } int *pa = test1(&a); printf("a= %d\n",a);
Arrays cannot be returned as function parameters.
Function pointer:
What is the type?
A type is a pointer to a function.
void show() { printf("Hello"); } printf("show = %d\n",show); //Print function name, function pointer void(*pShow)() = show;//pShow is a function pointer (*pShow)(); //Another way of writing pShow();
Pointer to function
typedef void (*PSWAP)(int *,int *) ;//Define a new type void (*pswap)(int *pa,int *pb) = swap;//Pointer to function pswap(&a,&b); PSWAP pswap = swap; //Alias printf("a=%d,b=%d,c=%d\n",a,b,c);