Knowledge summary of C language pointer advanced stage
I'll be there some day 2021-07-20 04:51:02
C Language advanced stage learning summary

Content

1-> Character pointer //char* p;

2-> Pointer array //int* arr[10];

3-> Array pointer //int (*p)[10] = &arr;

4-> Array parameter passing and pointer parameter passing //

5-> A function pointer //int (*pAdd)(int, int) = Add;

6-> Function pointer array //int (*pArr[])(int, int);

7-> A pointer to an array of function pointers   //int(* (*ppArr[]))(int, int) = &pArr;

8-> Callback function

9-> Pointer and array analysis of interview questions


1-> Character pointer

//1. Character pointer 
int main()
{
char arr[] = "abcdef";
char* pc = arr;
printf("%s\n", arr);
printf("%s\n", pc);
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
int main()
{
const char* p = "abcdef";//"abcdef" Is a constant string 
// Point to elements a
//*p = 'w';
//printf("&c\n",*p);
//printf("%s\n", p);
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.


2-> Pointer array

//2. Pointer array 
// It's an array , An array of pointers 
int main()
{
int arr[10] = { 0 };// Shape array 
char ch[5] = { 0 };// A character array 
int* parr[4];// An array of plastic pointers - Pointer array 
char pch[5];// An array of character pointers - Pointer array 
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
int main()
{
int arr1[] = { 1,2,3,4,5 };
int arr2[] = { 2,3,4,5,6 };
int arr3[] = { 3,4,5,6,7 };
int* parr[] = { arr1,arr2,arr3 };// First element address 
int i = 0;
for (i = 0; i < 3; i++)
{
int j = 0;
for (j = 0; j < 5; j++)
{
printf("%d ", *(parr[i] + j));
}
printf("\n");
}
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.


3-> Array pointer

//3. Array pointer 
// Is a pointer 
int main()
{
int* p = NULL; //p It's a plastic pointer - Pointer to the shape - Can store plastic address 
char* pc = NULL; //pc It's a character pointer = Pointer to character - An address where characters can be stored 
// Array pointer - Pointer to array - The address where the array is stored 
int arr[10] = { 0 };
//arr - First element address 
//&arr[0] - First element address 
//&arr - Array address 
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int(*p)[10] = &arr; // Block priority is high 
// above p It's an array pointer 
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int(*pa)[10] = &arr;
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", (*pa)[i]); //*pa == arr
}
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
int main()
{
int arr[3][5] = { {1,2,3,4,5},{2,3,4,5,6 },{3,4,5,6,7} };
print1(arr, 3, 5);//arr - The array name is the first element address 
// hold arr Think of it as a one-dimensional array 
// So the array arr There are three elements , The first element address is the first line address 
print2(arr, 3, 5);
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
int arr[5]; // 
int* parr1[10]; //parr1 Is an array , Array has 10 Elements , Each element is a int Type pointer , int* parr1[10] It's an array of pointers 
int (*parr2)[10]; //parr2 It's a pointer , The pointer points to an array , Array has 10 Elements , Each element is int type ,int (*parr2)[10] It's an array pointer 
int ((*parr3)[10])[5];
//parr3 Is an array , The array has 10 Elements , Each element is an array pointer , The array pointer points to an array with five elements , Each element is int
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.


4-> Array parameters 、 Pointer parameter

//4. Array parameters 、 Pointer parameter 
// One dimensional array parameters 
void test(int arr[])//ok
{}
void test(int arr[10])//ok
{}
void test(int *arr)//ok
{}
void test2(int *arr[20])//ok
{}
void test2(int **arr)//ok 
{}
int main()
{
int arr[10] = { 0 };
int *arr2[20] = { 0 };
test(arr);
test2(arr2);
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
// Two dimensional array parameters 
void test(int arr[3][5])
{}
void test2(int arr[][5])
{}
void test3(int arr[3][]) //err, You can't omit Columns 
{}
void test4(int* arr) //err, It's an array Out-of-service int*
{}
void test5(int **arr)//err, The secondary pointer cannot receive either An array address 
{}
void test6(int (*arr)[5])
{}
int main()
{
int arr[3][5] = { 0 };
test(arr);// Two dimensional array parameters 
test2(arr);
test3(arr);
test4(arr);
test5(arr);
test6(arr);
return 0;
}
// summary : Two dimensional array parameters , Function parameter design can only omit the first [] The number of .
// Because for a two-dimensional array , I don't know how many lines there are , But you have to know how many elements in a row 
// So it's easy to calculate 
// The secondary pointer transmits parameters 
void test(int** ptr)
{
printf("num = %d\n", **ptr);
}
int main()
{
int n = 10;
int* p = &n;
int** pp = &p;
test(pp);
test(&p);
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.


5-> A function pointer

//5. A function pointer - Is a pointer to a function - A pointer to the function address 
int Add(int x, int y)
{
int z = 0;
z = x + y;
return z;
}
int main()
{
int a = 10;
int b = 20;
int arr[10] = { 0 };
int (*p)[10] = &arr;
//printf("%d\n", Add(a, b));
//& Function name and Function name All are the addresses of functions 
/*
printf("%p\n", &Add);
printf("%p\n", Add);
*/
int (*pa)(int, int) = Add; // A function pointer 
printf("%d\n ", (*pa)(2, 3));
return 0;
}
void Print(char*str)
{
printf("%s\n", str);
}
int main()
{
void (*p)(char*) = Print;
(*p)("hello bit");
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
// Analysis of the code 1
(*(void (*)())0)();
void(*)() - Function pointer type
hold 0 Force type to :void(*)() - 0 Is the address of a function
call 0 The function at the address
// Code 2
void (*signal(int, void(*)(int)))(int);
void(* )(int);
signal(int, void(*)(int))
signal It's a function declaration - There are two parameters , One is int, The second is function pointers , The function pointer points to a function whose argument is int, The return type is void
signal The return type of a function is also a function pointer : The function pointer points to a function whose argument is int, The return type is void
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
int Add(int x, int y)
{
int z = 0;
z = x + y;
return z;
}
int main()
{
int a = 10;
int b = 20;
int (*pa)(int, int) = Add; // A function pointer 
// All three of the following are true 
printf("%d\n ", (*pa)(2, 3));
printf("%d\n ", pa(2, 3));
printf("%d\n ", Add(2, 3));
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.


6-> Function pointer array

//6. Function pointer array - An array of the addresses of functions 
int Add(int x, int y)
{
return x+y;
}
int Sub(int x, int y)
{
return x - y;
}
int Mul(int x, int y)
{
return x * y;
}
int Div(int x, int y)
{
return x / y;
}
int main()
{
// Pointer array 
int* arr[5];
// You need an array , This array can hold 4 The address of a function - An array of function pointers 
int (*pa)(int, int) = Add; //Sub/Mul/Div
int (*parr[4])(int, int) = {Add,Sub, Mul,Div};// An array of function pointers 
int i = 0;
for (i = 0; i < 4; i++)
{
printf("%d\n",parr[i](2, 3));
}
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
// Test questions 
char* my_strcpy(char* dest, const char* src);
//1. Write a function pointer pf, Can point to my_strcpy
char* (*pf)(char*, const char*);
//2. Write an array of function pointers pfArr, Be able to store 4 individual my_strcpy Address of function 
char* (*pfArr[4])(char*, const char*);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
// Function pointer case 
// Calculator 
void menu()
{
printf("1.add\n2.sub\n3.Mul\n4.div\n");
}
int Add(int x, int y)
{
return x+y;
}
int Sub(int x, int y)
{
return x - y;
}
int Mul(int x, int y)
{
return x * y;
}
int Div(int x, int y)
{
return x / y;
}
int Div(int x, int y)
{
return x ^ y;
}
int main()
{
int input = 0;
int x = 0;
int y = 0;
do
{
menu();
printf(" Please select :>");
scanf("%d",&input);
switch(input)
{
case 1:
printf(" Please enter two operands :>");
scanf("%d%d", &x, &y);
printf("%d\n", Add(x, y));
break;
case 2:
printf(" Please enter two operands :>");
scanf("%d%d", &x,&y);
printf("%d\n", Sub(x, y));
break;
case 3:
printf(" Please enter two operands :>");
scanf("%d%d", &x, &y);
printf("%d\n", Mul(x, y));
break;
case 4:
printf(" Please enter two operands :>");
scanf("%d%d", &x, &y);
printf("%d\n", Div(x, y));
break;
case 0:
printf(" sign out \n");
break;
default:
printf(" Wrong choice \n");
break;
}
} while (input);
}
// Method 2. Using an array of function pointers 
void menu()
{
printf("1.add\n2.sub\n3.Mul\n4.div\n5.Xor");
}
int Add(int x, int y)
{
return x + y;
}
int Sub(int x, int y)
{
return x - y;
}
int Mul(int x, int y)
{
return x * y;
}
int Div(int x, int y)
{
return x / y;
}
int Xor(int x, int y)
{
return x ^ y;
}
int main()
{
int input = 0;
int x = 0;
int y = 0;
//pfArr Is an array of function pointers - purpose : Transfer table 
int (*pfArr[])(int, int) = { 0,Add,Sub,Mul,Div,Xor};
do
{
menu();
printf(" Please select :>");
scanf_s("%d", &input);
if (input >= 1 && input <= 5)
{
printf(" Please enter the operands :>");
scanf_s("%d%d", &x, &y);
int ret = pfArr[input](x, y);
printf("%d\n", ret);
}
else if (input == 0)
{
printf(" sign out \n");
}
else
{
printf(" Wrong choice !\n");
}
} while (input);
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.


7-> A pointer to an array of function pointers

//7. A pointer to an array of function pointers 
int Add(int x, int y)
{
return x + y;
}
int main()
{
int arr[10] = { 0 };
int (*p)[10] = &arr; // Take the address of the array 
int (*pfArr[4])(int, int);//pfArr - Is an array - An array of function pointers 
//ppfArr It's a point Function pointer array The pointer to 
int(*(*ppfArr)[4])(int, int) = &pfArr;
//
//ppfArr It's an array pointer , The array that the pointer points to is 4 Elements 
// The type of each element of the array pointed to is a function pointer int(*)(int, int)
//
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.


8-> Callback function

//8. Callback function - Functions called through function pointers 
void print(const char *str)
{
printf("hehe:%s", str);
}
void test(void (*p)(char*))
{
printf("test\n");
p("bit");
}
int main()
{
test(print);
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
//void Pointer to type 
int main()
{
int a = 10;
//int* pa = &a;
//char* pc = &a;
//char ch = 'w';
void* p = &a; // No type pointer - Can receive any type of address - Cannot dereference - You can't add or subtract integers 
p = &ch;
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
//qsort - Library function - Sort - You can sort any type of data 
//quick sort
//void qsort(
// void* base, // Address of the first element of the target array 
// size_t num, // The number of array elements to be sorted 
// size_t width, // The size of each element - Unit is byte 
// int (*cmp)(const void* e1, const void* e2) // A function pointer , Compare the address of the function used by the two elements - This function is implemented by the user himself - The two arguments to the function pointer are : The addresses of the two elements to be compared 
//); 
// Yes arr Sort , In ascending order 
// Bubble sort functions can only sort integer arrays 
//bubble_sort(arr, sz);// Bubble sort function 
//void bubble_sort(int arr[], int sz)
{
// Determine the number of times to bubble sort 
int i = 0;
for (i = 0; i < sz - 1; i++)
{
int flag = 1;// Suppose that the data to be sorted this time is in order 
// Every bubble sort 
int j = 0;
for (j = 0; j < sz - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
flag = 0;
}
}
if (flag == 1)
{
break;
}
}
}
struct stu
{
char name[20];
int age;
};
int cmp_int(const void* e1, const void* e2) //void* - Can receive any type of address 
{
// Compare the values of two shapes 
return *(int*)e1 - *(int*)e2;
}
int cmp_float(const void* e1, const void* e2)
{
// Compare two floating-point types 
return ((int)(*(float*)e1 - *(float*)e2));
}
int cmp_stu_by_age(const void* e1, const void* e2)
{
// Compare two ages 
return ((struct stu*)e1)->age - ((struct stu*)e2)->age;
}
int cmp_stu_by_name(const void* e1, const void* e2)
{
// Comparing names is comparing strings 
// String comparison cannot be used directly ><= To compare , Should use the strcmp function 
return strcmp(((struct Stu*)e1)->name ,((struct Stu*)e2)->name);
}
void test1()
{
int arr[10] = { 9,8,7,6,5,4,3,2,1,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
qsort(arr, sz, sizeof(arr[0]), cmp_int);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
}
void test2()
{
float f[] = { 9.0, 8.0, 7.0, 6.0, 5.0, 4.0 };
int sz = sizeof(f) / sizeof(f[0]);
qsort(f, sz, sizeof(f[0]), cmp_float);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%f ", f[i]);
}
}
void test3()
{
struct stu s[3] = { {"zhangsan",20},{"lisi",30},{"wangwu",10} };
int sz = sizeof(s) / sizeof(s[0]);
qsort(s, sz, sizeof(s[0]), cmp_stu_by_age);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", s[i]);
}
}
void test4()
{
struct stu s[3] = { {"zhangsan",20},{"lisi",30},{"wangwu",10} };
int sz = sizeof(s) / sizeof(s[0]);
qsort(s, sz, sizeof(s[0]), cmp_stu_by_name);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%s ", s[i]);
}
}
// Realization bubble_sort Function programmers , Does he know the type of data to sort in the future - I do not know! 
// The programmer doesn't know , The types of the two elements to be compared 
void Swap(char* buf1, char* buf2, int width)
{
int i = 0;
for (i = 0; i < width; i++)
{
char tmp = *buf1;
*buf1 = *buf2;
*buf2 = tmp;
buf1++;
buf2++;
}
}
void bubble_sort(void* base, int sz, int width, int (*cmp)(const void* e1, const void* e2))
{
int i = 0;
for (i = 0; i < sz - 1; i++)
{
// Every bubble sort 
int j = 0;
for (j = 0; j < sz - 1 - i; j++)
{
// A comparison of the two elements 
if (cmp((char*)base+j*width,(char*)base+(j+1)*width) > 0)
{
// In exchange for 
Swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
}
}
}
}
int cmp_stu_by_age(const void* e1, const void* e2)
{
// Compare two ages 
return ((struct stu*)e1)->age - ((struct stu*)e2)->age;
}
void test5()
{
struct stu s[3] = { {"zhangsan",20},{"lisi",30},{"wangwu",10} };
int sz = sizeof(s) / sizeof(s[0]);
// Use bubble_sort A good programmer must know what data he is sorting 
// You should know how to compare the elements in the array to be sorted 
bubble_sort(s, sz, sizeof(s[0]),cmp_stu_by_age);
}
int main()
{
// adopt qsort Sort 
test1(); // Sort integer arrays 
test2(); // Sort floating point arrays 
test3(); // Sort - By the age of the structure array 
test4(); // Sort - Through the name of the structure array 
test5(); // Modelled on the qsort To modify the bubble function implementation to sort any type 
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.


9-> Pointer and array analysis of interview questions

//9. Pointer and array analysis of interview questions 
int main()
{
// The array name is the first element address 
// exception 
//1.sizeof( Array name ) - The array name represents the entire array 
//2.& Array name - The array name represents the entire array 
//
// One dimensional array 
int a[] = { 1,2,3,4 };
printf("%d\n", sizeof(a)); //sizeof( Array name ) - It's the total size of the array - Unit is byte -16
printf("%d\n", sizeof(a + 0));// 4/8 - The array name here represents the value of the first element ,a+0 Or the first element address , The size of the address is 4/8 Bytes 
printf("%d\n", sizeof(*a)); //4 - The array name represents the address of the first element ,*a It's the first element 
printf("%d\n", sizeof(a + 1));// 4/8 - The address of the second element , The size of the address is 4/8 Bytes 
printf("%d\n", sizeof(a[1])); //4 - The first 2 The size of an element 
printf("%d\n", sizeof(&a)); // 4/8 - &a What we get is the address of the array , But the array address is also the address , The size of the address is 4/8 Bytes 
printf("%d\n", sizeof(*&a)); //16
printf("%d\n", sizeof(&a + 1)); // 4/8
printf("%d\n", sizeof(&a[0])); // 4/8
printf("%d\n", sizeof(&a[0] + 1));// 4/8
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
// A character array 
int main()
{
char arr[] = { 'a','b','c','d','e','f' };
printf("%d\n", sizeof(arr));; //6
printf("%d\n", sizeof(arr + 0));// 4/8
printf("%d\n", sizeof(*arr)); // 1
printf("%d\n", sizeof(arr[1])); //1
printf("%d\n", sizeof(&arr)); //4/8
printf("%d\n", sizeof(&arr + 1)); // 4/8
printf("%d\n", sizeof(&arr[0] + 1)); // 4/8
printf("%d\n", strlen(arr)); // Random value 
printf("%d\n", strlen(arr + 0));// Random value 
//printf("%d\n", strlen(*arr)); // err
//printf("%d\n", strlen(arr[1])); // err
printf("%d\n", strlen(&arr)); // Random value 
printf("%d\n", strlen(&arr + 1)); // Random value -6
printf("%d\n", strlen(&arr[0] + 1)); // Random value -1
return 0;
}
int main()
{
char arr[] = "abcdef";
printf("%d\n", sizeof(arr)); // 7 
printf("%d\n", sizeof(arr + 0));// 4/8
printf("%d\n", sizeof(*arr)); //1
printf("%d\n", sizeof(arr[1])); //1
printf("%d\n", sizeof(&arr)); // 4/8
printf("%d\n", sizeof(&arr + 1));// 4/8
printf("%d\n", sizeof(&arr[0] + 1));// 4/8
printf("%d\n", strlen(arr)); // 6
printf("%d\n", strlen(arr + 0));// 6
//printf("%d\n", strlen(*arr)); //err
//printf("%d\n", strlen(arr[1])); //err
printf("%d\n", strlen(&arr)); // 6
printf("%d\n", strlen(&arr + 1));// Random value 
printf("%d\n", strlen(&arr[0] + 1));// 5
return 0;
}
int main()
{
const char* p = "abcdef";
printf("%d\n", sizeof(p)); // 4/8
printf("%d\n", sizeof(p + 1)); // 4/8
printf("%d\n", sizeof(*p)); // 1
printf("%d\n", sizeof(p[0])); // 1 p[0] == *(p + 0) == 'a'
printf("%d\n", sizeof(&p)); // 4/8
printf("%d\n", sizeof(&p + 1)); // 4/8
printf("%d\n", sizeof(&p[0] + 1));// 4/8
printf("%d\n", strlen(p)); // 6
printf("%d\n", strlen(p + 1)); // 5
//printf("%d\n", strlen(*p)); // err
//printf("%d\n", strlen(p[0])); // err p[0] == *(p + 0) == 'a'
printf("%d\n", strlen(&p)); // Random value 
printf("%d\n", strlen(&p + 1)); // Random value 
printf("%d\n", strlen(&p[0] + 1));// 5
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
// Two dimensional array 
int main()
{
int a[3][4] = { 0 };
printf("%d\n", sizeof(a)); // 48
printf("%d\n", sizeof(a[0][0]));//4
printf("%d\n", sizeof(a[0])); //16 a[0] The first line is the name of the one-dimensional array == sizeof( Array name )
printf("%d\n", sizeof(a[0] + 1));// 4/8 a[0] It's the address of the element at the beginning of the first line a[0]+1, The address of the second element in the first line 
printf("%d\n", sizeof(*(a[0] + 1)));//4
printf("%d\n", sizeof(a + 1)); //4/8 a Is the address of the first element of a two-dimensional array , The first element of a two-dimensional array is the first line , And the binary array as a one-dimensional array ,a+1 It's the address on the second line 
printf("%d\n", sizeof(*(a + 1)));//16
printf("%d\n", sizeof(&a[0] + 1));//4/8 Second line address 
printf("%d\n", sizeof(*(&a[0] + 1)));//16
printf("%d\n", sizeof(*a)); //16 *a = First line address dereference 
printf("%d\n", sizeof(a[3])); //16 Represents the fourth line ,sizeof Not involved in real Computing 
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
// Pointer written test questions 
int main()
{
int a[5] = { 1,2,3,4,5 };
int* ptr = (int*)(&a + 1); //&a + 1 by 5 Back address 
printf("%d, %d\n", *(a + 1), *(ptr - 1)); //2 5 
return 0;
}
struct Test
{
int Num;
char* pcName;
short sDate;
char cha[2];
short sBa[4];
}*p; // Structure pointer p
// hypothesis p The value of is ox100000, What are the values of the expressions in the following table ?
// It is known that , Structure Test The variable size of type is 20 Bytes 
int main()
{
p = (struct Test*)0x100000;
printf("%p\n", p + 0x1); //0x00100014 
printf("%p\n", (unsigned long)p + 0x1); //0x00100001
printf("%p\n", (unsigned int*)p + 0x1); //0x00100004
return 0;
}
int main()
{
int a[4] = { 1,2,3,4 };
int* ptr1 = (int*)(&a + 1);
int* ptr2 = (int*)((int)a + 1);
printf("%x,%x", ptr1[-1], *ptr2);// 4 0x02000000
return 0;
}
int main()
{
int a[3][2] = { (0,1),(2,3),(4,5) };// Comma expression 1,3,5
int* p;
p = a[0]; // first line 1, 3
printf("%d", p[0]); //1 
return 0;
}
int main()
{
int a[5][5];
int(*p)[4];
p = a;//int (*)[4] ---- int (*)[5]
printf("%p,%d\n", &p[4][2] - &a[4][2], &p[4][2] - &a[4][2]);//0xfffffffc , 4
return 0;
}
int main()
{
int aa[2][5] = { 1,2,3,4,5,6,7,8,9,10 };
int* ptr1 = (int*)(&aa + 1);
int* ptr2 = (int*)(*(aa + 1)); //*(aa + 1) == aa[1]
printf("%d,%d\n", *(ptr1 - 1), *(ptr2 - 1)); // 10 5
return 0;
}
int main()
{
char* a[] = { "work","at","alinbaba" };
char** pa = a;
pa++;
printf("%s\n", *pa);//at
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.


Please bring the original link to reprint ,thank
Similar articles

2021-07-20

2021-07-20

2021-07-20