Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it
Tip: the following is the main content of this article. The following cases can be used for reference
1, What's the use of pointers
1. Pointer application scenario 1
Exchange the values of two variables
#include <stdio.h> void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; } int main(int argc, char const *argv[]) { int a = 5; int b = 6; printf("a = %d; b = %d\n", a, b); swap(&a, &b); printf("a = %d; b = %d", a, b); return 0; } }
2. Pointer application scenario 2
- Function returns multiple values, and some values can only be returned through pointers
#include <stdio.h> void minmax(int a[], int len, int *min, int *max) { int i; *min = *max = a[0]; for (i = 1; i < len; i++) { if (a[i] < *min) { *min = a[i]; } if (a[i] > *max) { *max = a[i]; } } } int main(void) { int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 13, 14, 16, 17, 21, 23, 55, }; int min, max; minmax(a, sizeof(a) / sizeof(a[0]), &min, &max); printf("min=%d , max=%d\n", min, max); return 0; }
- The parameters passed in are actually variables that need to save the results brought back (return pointer header)
2, Why is the sizeof after the array is passed into the function wrong?
1. What does the array passed into the function become?
It can be seen from the results that after the array is passed into the function, it actually becomes a pointer. He said that the address of the first position of the array is passed into the function.
- sizeof(a) == sizeof(int*)
- But you can operate with the operator [of the array
Four equivalent ways to write arrays as function parameters
3, Pointer and const: both the pointer and the variable referred to by the pointer may be const
1. The pointer is const
- Indicates that once the address of a variable is obtained, it can no longer point to other variables
- int* const q = &i; // Q is const
- *q = 26 // OK
- q++ //ERROR
2. The variable referred to by the pointer is const
- Indicates that the variable cannot be modified through this pointer (it does not make the variable const)
- const int *p = &i;
- *p = 26 // ERROR! (*p) is const
- i = 26; //OK
- p = &j; //OK
3. Combination of const and pointer
int i; const int* p1 = &i; 1 int const* p2 = &i; 2 int *const p3 = &i; 3
Determine which flag is const in front of or behind *
- For example, 1 and 2, const is in front of *, that is, the indicated variable cannot be modified through * p1 and * p2
- For example, 3, const is after *, that is, p3 cannot be changed, and the pointer address cannot be changed to other addresses
4.const array
- const int a[] = {1,2,3,4,5,6};
- The array variable is const. One more const in front of the array definition indicates that each cell of the array is const int
- Therefore, the array defined in this way must be initialized
Usage of const type array
4, Pointer operation
1. Pointer addition
int *p; int a[] = {1,2,3,4}; p = a; p++; //It means: p + + points to the next bit. Now p points to a[1], because it is of type int. when the address points to the next bit, that is, address + 4 If it is char, the address is + 1, and the operation of the address depends on the variable type
As for why different types have different address changes, the reason is that if the pointer does not point to a continuously allocated space, such as an array, this operation is meaningless.
2. Pointer subtraction
#include <stdio.h> int main(void) { int a[] = {1, 2, 3, 4, 5, 6}; int *p = a; int *q = a + 5; // printf("%d\n", *p); // printf("%d\n", *q); printf("q - p = %d", q - p); //q-p = address value subtracted / sizeof(int) return 0; }
The pointer is subtracted to obtain the phase difference digit.
3. Pointer comparison
4. 0 address in memory
5. Difference of pointer type
Because different types have different digits. For example, int is 4 bits and char is 1 bit.
6. Conversion of pointer type
5, Dynamic memory allocation through pointer
After malloc allocates memory, it returns a pointer of void * type.
#include <stdio.h> int main(void) { int number = 100; int *p = (int *)malloc(sizeof(int)); free(p); }
6, String operation
1. Single character input and output
Usage of putchar
Usage of getchar
#include <stdio.h> int main(void) { int ch; while ((ch = getchar()) != EOF) { putchar(ch); } return 0; }
2. String array
As can be seen from the figure, char **a is not what we want, and char a[][n] (n required) should be used for string array.
#include <stdio.h> int main(void) { char a[][10] = { "hhh", "sad", "saddaasd"}; return 0; }
7, Implementation of string function
strlen
#include <stdio.h> #include <string.h> int main(void) { char *a = "helloworld"; printf("%d", strlen(a)); return 0; }
strcmp
strcmp, which compares each bit of two strings bit by bit.
#include <stdio.h> #include <string.h> int main(void) { char *a = "helloworld"; char *b = "asdvvsddds"; printf("%d", strcmp(a, b)); return 0; }
strcpy
The returned address is the address of the first character found. If you want to find the second, you can find the first address + 1, and then use the function again.
Strict direct handling. Indicate the source.