1.理解数组指针
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i;
int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int(*p)[10] = &arr;
for (i = 0; i < 10; i++)
{
printf("%d ", (*p)[i]);
}
putchar('\n');
system("pause");
return 0;
}
2.理解函数指针和定义
#include<stdio.h>
#include<stdlib.h>
int Add(int a, int b)
{
return a + b;
}
int main()
{
int (*p)(int, int) = Add;
printf("%d\n", (*p)(1, 2));
system("pause");
return 0;
}
3.理解函数指针数组和定义,转移表
#include<stdio.h>
#include<stdlib.h>
int Add(int a, int b)
{
return a + b;
}
int Sub(int a, int b)
{
return a - b;
}
int Mul(int a, int b)
{
return a * b;
}
int Div(int a, int b)
{
return a / b;
}
int main()
{
int i;
int(*p[5])(int, int) = { Add, Sub, Mul, Div };
for (i = 0; i < 4; i++)
{
printf("%d ", (*p[i])(1, 2));
}
putchar('\n');
system("pause");
return 0;
}
4.理解指向函数指针数组的指针和定义
#include<stdio.h>
#include<stdlib.h>
int Add(int a, int b)
{
return a + b;
}
int Sub(int a, int b)
{
return a - b;
}
int Mul(int a, int b)
{
return a * b;
}
int Div(int a, int b)
{
return a / b;
}
int main()
{
int i;
int(*p[5])(int, int) = { Add, Sub, Mul, Div };
int (*(*pa)[5])(int, int) = &p;
for (i = 0; i < 4; i++)
{
printf("%d ", (*pa)[i](1, 2));
}
putchar('\n');
system("pause");
return 0;
}
5.理解回调函数的使用
#include<stdio.h>
#include<stdlib.h>
int CompareInt(const void* num1, const void* num2)
{
return *(int*)num1 - *(int*)num2;
}
int main()
{
int i, n;
int arr[] = { 7, 8, 5, 1, 2, 1, 0, 3, 4, 9 };
n = sizeof(arr) / sizeof(arr[0]);
qsort(arr, n, sizeof(int), CompareInt);
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
putchar('\n');
system("pause");
return 0;
}