#define _CRT_SECURE_NO_WARNINGS
#include
int check_sys()
{
int a = 1;//0x00000001
char* p = (char*)&a;//int*
return *p;//返回1表示小端,返回0表示大端
}
int main()
{
//写代码判断当前机器的字节序
int ret = check_sys();
if (ret == 1)
{
printf("小端\n");
}
else
{
printf("大端\n");
}
return 0;
}
一个数的存入和它的取出是息息相关的
#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
int n = 9;
float* pFloat = (float*)&n;
printf("n的值为:%d\n", n);
printf("*pFloat的值为:%f\n", *pFloat);
*pFloat = 9.0;
printf("num的值为:%d\n", n);
printf("*pFloat的值为:%f\n", *pFloat);
return 0;
}
E为一个无符号整数(unsigned int)
情况一:E不全为0或不全为1
情况二:E全为0
情况三:E全为1
#define _CRT_SECURE_NO_WARNINGS 1
#include
int main()
{
int n = 9;
float* pFloat = (float*)&n;
printf("n的值为:%d\n", n);
printf("*pFloat的值为:%f\n", *pFloat);
*pFloat = 9.0;
printf("num的值为:%d\n", n);
printf("*pFloat的值为:%f\n", *pFloat);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
int main()
{
char ch = 'q';
char * pc = &ch;
char* ps = "hello bit";
char arr[] = "hello bit";
*ps = 'w';//err
arr[0] = 'w';
printf("%c\n", *ps);//h
printf("%s\n", ps);//hello bit
printf("%s\n", arr);//wello bit
return 0;
}
char* ps = "hello bit";不是把字符串 hello bit放到字符指针 pstr 里了,而是把"hello bit"这个字符串的首字符的地址存储在了ps中
"hello bit"是一个常量字符串,常量字符串是不能被修改,则*ps = 'w';这个语句就是错的
#define _CRT_SECURE_NO_WARNINGS 1
#include
int main()
{
char str1[] = "hello bit.";
char str2[] = "hello bit.";
const char* str3 = "hello bit.";
const char* str4 = "hello bit.";
//*str3 = 'w';
if (str1 == str2)
printf("str1 and str2 are same\n");
else
printf("str1 and str2 are not same\n");
if (str3 == str4)
printf("str3 and str4 are same\n");
else
printf("str3 and str4 are not same\n");
return 0;
}
str1和str2不同,str3和str4相同。
传入的参数是二维数组的首地址
#define _CRT_SECURE_NO_WARNINGS 1
#include
int Add(int x, int y)
{
return x + y;
}
int main()
{
//int (*pf)(int, int) = &Add;//OK
int (*pf)(int, int) = Add;//Add === pf
int ret = 0;
ret = (*pf)(3, 5);//1
printf("%d\n", ret);
ret = pf(3, 5);//2
printf("%d\n", ret);
ret = Add(3, 5);//3
printf("%d\n", ret);
//int ret = * pf(3, 5);//err
return 0;
}
代码1 : (*(void (*)())0)();// 请问该代码什么意思
代码2 :void (*signal(int , void(*)(int)))(int);// 请问该代码什么意思
#define _CRT_SECURE_NO_WARNINGS 1
#include
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;
}
void menu()
{
printf("**************************\n");
printf("**** 1. add 2. sub ****\n");
printf("**** 3. mul 4. div ****\n");
printf("**** 0. exit ****\n");
printf("**************************\n");
}
int main()
{
int input = 0;
//计算器-计算整型变量的加、减、乘、除
//a&b a^b a|b a>>b a<b
do {
menu();
int (*pfArr[5])(int, int) = { NULL, Add, Sub, Mul, Div };
int x = 0;
int y = 0;
int ret = 0;
printf("请选择:>");
scanf("%d", &input);//2
if (input >= 1 && input <= 4)
{
printf("请输入2个操作数>:");
scanf("%d %d", &x, &y);
ret = pfArr[input](x, y);
printf("ret = %d\n", ret);
}
else if (input == 0)
{
printf("退出程序\n");
break;
}
else
{
printf("选择错误\n");
}
} while (input);//只有输入0才退出
return 0;
}
将一个函数A的地址传给另一个函数B(用函数指针接收),该函数B又通过解引用调用其他函数
#define _CRT_SECURE_NO_WARNINGS 1
#include
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;
}
void menu()
{
printf("**************************\n");
printf("**** 1. add 2. sub ****\n");
printf("**** 3. mul 4. div ****\n");
printf("**** 0. exit ****\n");
printf("**************************\n");
}
int Calc(int (*pf)(int, int))
{
int x = 0;
int y = 0;
printf("请输入2个操作数>:");
scanf("%d %d", &x, &y);
return pf(x, y);
}
int main()
{
int input = 0;
//计算器-计算整型变量的加、减、乘、除
//a&b a^b a|b a>>b a<b
do {
menu();
int ret = 0;
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
ret = Calc(Add);
printf("ret = %d\n", ret);
break;
case 2:
ret = Calc(Sub);
printf("ret = %d\n", ret);
break;
case 3:
ret = Calc(Mul);//
printf("ret = %d\n", ret);
break;
case 4:
ret = Calc(Div);//
printf("ret = %d\n", ret);
break;
case 0:
printf("退出程序\n");
break;
default:
printf("选择错误,重新选择!\n");
break;
}
} while (input);
return 0;
}
考查的是:指针类型决定了指针的运算
#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
int a[5][5];
int(*p)[4];
p = a;
printf("%p,%d\n", &p[4][2] - &a[4][2], &p[4][2] - &a[4][2]);//FFFFFFFC,-4
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
char* c[] = { "ENTER","NEW","POINT","FIRST" };
char** cp[] = { c + 3,c + 2,c + 1,c };
char*** cpp = cp;
printf("%s\n", **++cpp);//POINT
printf("%s\n", *-- * ++cpp + 3);//ER
printf("%s\n", *cpp[-2] + 3);//ST
printf("%s\n", cpp[-1][-1] + 1);//EW
return 0;
}