1. In the C language, you can use the const keyword to modify the pointer to limit the access to the memory space pointed to by the pointer .
int *const ptr3 = arr;
*ptr3 = 998;
ptr3 points to the array arr, the value of the element pointed to by ptr3 can be modified, but the element pointed to by ptr3 cannot be modified, for example: ptr = arr; ×
int const *ptr = &a; //Constant target pointer, can only be replaced but cannot modify the value,
ptr =&b;
2. Convenient way of writing bubble sort
for (i = 0; i < 3; ++i){ // Sort the array using bubble sort
for (j = 0; j < 2 - i; ++j){
if (arr[j] > arr[j + 1])arr[j] ^= arr[j + 1] ^= arr[j] ^= arr[j + 1];
}
}
3. Measure the length of the array
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int sz = sizeof(arr) / sizeof(arr[0]);
4. Bitwise operators
Discard on the left, add 0 to the right;
int a = 2;
int b = a << 1; // Shift the binary bit of a to the left by 1 bit; b is 4,
a : 00000000000000000000000000000010
b : 0|000000000000000000000000000010+0
The right side is discarded, and the left side is filled with 0;
int a = 10;
int b = a >> 1;
a : 00000000000000000000000000001010
b : 0+0000000000000000000000000000101|0
5. Negative storage
The negative number -1 should be stored in the memory, and the memory is stored in two's complement (255 - 1)
6. Bitwise operators
7. The difference between structure accessors:
definition:
struct student { char name[20]; int age; };
struct student stu = {"Alice", 18};
Point operation: (access structure members through structure variable names)
printf("Name: %s, Age: %d\n", stu.name, stu.age);
Arrow operation: (access structure members through structure pointers)
struct student *pstu = &stu;
printf("Name: %s, Age: %d\n", pstu->name, pstu->age);
8. Pointer addition and subtraction
Avoid wild pointers, int* p = NULL; // Use when you don't know what value to give, even if you assign NULL
9. The role of pointer subtraction
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
printf("%d\n", &arr[9] - &arr[0]); // Get the number of elements between the pointer and the pointer
10. Determine the size of the system endian
Because when a little-endian machine stores multi-byte data, the low byte comes first and the high byte follows, that is, 0x01020304 is stored as 0x04030201 in a little-endian machine. If the low byte of a is not 1, it means that the byte order of the current machine is big endian.
int a = 1;
char* pa = (char*)&a; // Here you need to perform a forced type conversion
if(*pa == 1)printf("little endian");
else printf("big endian");
11. Unsigned type unsigned____
The range of signed char is: -128 ~ 127
The range of unsigned char is: 0 ~ 255
12. Storage of floating point numbers
The binary floating-point number V can be expressed in the following form: (-1)^S * M * 2^E
① (-1)^s represents the sign bit, when s = 0, V is a positive number; when s = 1, v is a negative number
② M represents a valid number, greater than or equal to 1, less than 2
③ 2^E means exponent
Example: Binary: 101.1 → 1.011 * 2^2 → (-1) ^0 * 1.011 * 2^2
Save as: s=0 M=1.011 E=2
13. How to write array pointer
for(i=0; i<10; i++)
{ //The following expressions are all equivalent printf("%d ", p[i]); printf("%d ", *(p+i)); printf( "%d ", *(arr+i)); printf("%d ", arr[i]); //arr[i] == *(arr+i) == *(p+i) == p[i] }
Array of pointers: int* parr1[10];
Array pointer: int (*parr2)[10];
Array of array pointers: int (*parr3[10])[5];
14. Two-dimensional array parameter passing
15. Function pointer
int Add(int x, int y)return x + y;
int (*pf)(int, int) = &Add; //function pointer
Array of function pointers:
pfArr is an array of function pointers, and the inside of {} is the function name
int (*pfArr[5])(int, int) = {NULL, Add, Sub, Mul, Div};
use:
int input =0;
for(input =1;input<5;input++) (pfArr[input])(x, y); //Execute each function separately
16. Callback function of C language
void Calc(int (*pf)(int, int))
{ int x = 0; int y = 0; printf("Please enter 2 operands:>"); scanf("%d %d", &x, &y); printf("%d\n", pf(x, y)); }
Use: In other functions: Calc(Add);//Add is a new function
17. Notes on the use of pointers
If not initialized : int* p = NULL; // initialized to NULL
Dereferencing of pointers : Before dereferencing, it is necessary to ensure that the memory block pointed to by the pointer has been allocated.
if (p == NULL) {return 1;}
Free the pointer after use :
free(p); // release the memory block
p = NULL; // assign the pointer to NULL
You need to ensure that the types of the pointers match :
int a = 10;
float b = 3.14;
int* p = &a; // pointer of type int points to variable of type int
float* q = &b; // pointer of type float points to variable of type float
18. C language operation register
Assign a value of 1 to bit 6 of a register:
register:
19. Macro definition with parameters
To avoid being affected by the precedence of braces, semicolons, operators, etc.
20. Detailed use of pointers
1.
char ch = 'a';
char *p = &ch; ------->*p == p[0] ='a';
2.
char ch[] = {'a','\0'}; -----------ch is equal to &ch[0] ------->*ch==ch[0]= ='a'
3.
int a;
int *p = &a;
a = 10;
*p = 9;
printf("a = %d\n", a); // output is 9
printf("&a = %d\n", &a); // Output the address of a, that is, the address of variable a in memory
printf("p = %d\n", p); // Output the value of p, that is, the address of a
printf("p = %d\n", &p); // Output the address of p, the output result is 4 in the 32-bit system, and the output result is 8 in the 64-bit system
printf("*p = %d\n", *p); // Output the value pointed to by p, that is, the value of a, and the output result is 9
4.
int b[5] = {1, 2, 3, 4, 5};
printf("b = %d ,*b = %d, *(b+1) = %d, *(b+2) = %d\n",b, *b, *(b+1), *(b+2));
//b = -13328 ,*b = 1, *(b+1) = 2, *(b+2) = 3
5. String array output mode
char *str = "I love U";
int i, length;
length = strlen(str);
for (i = 0; i < length; i++)
{
printf("%c", str[i]);
}
//I love U
6.
char arr[] = "abcdef";
char *p = arr;
printf("*p %c\n", *p); // a
printf("p %c\n", p[0]); // abcdef
printf("p %s\n", p); // abcdef
printf("arr %s\n", arr); // abcdef
printf("&p %d\n", &p); //-13312