S1E21:指针
本帖最后由 BaysideLizard 于 2023-11-14 22:21 编辑#include <stdio.h>
//DNX写于2023年10月5日
int main()
{
char a ='Z';
int b = 123;
char *pa = &a;
int *pb = &b;
printf("a = %c\n",*pa);
printf("b = %d\n",*pb);
//通过指针间接访问数据
*pa = 'H';
*pb += 1;
printf("Now,a = %c\n",*pa);
printf("Now,b = %d\n\n",*pb);
//Dev C++中指针变量的大小都是8字节
printf("Sizeof a = %d,sizeof pa =%d\n",sizeof(a),sizeof(pa));
printf("Sizeof b = %d,sizeof pb =%d\n\n",sizeof(b),sizeof(pb));
//不加取值运算符,则输出指针的值,用%p打印地址类型
printf("The address of pa is %p\n",pa);
printf("The address of pb is %p\n",pb);
/*避免访问未初始化的指针
eg. int *m; 未初始化的指针
*m = 123; 此操作非常危险
*/
return 0;
}
运行结果:
a = Z
b = 123
Now,a = H
Now,b = 124
Sizeof a = 1,sizeof pa =8
Sizeof b = 4,sizeof pb =8
The address of pa is 000000000062FE0F
The address of pb is 000000000062FE08
--------------------------------
Process exited after 0.03476 seconds with return value 0
请按任意键继续. . .
在FishC学C的第八天
加油! 每天鼓励你一下
页:
[1]