//使用取地址运算符&取出变量的地址,在屏幕上显示
#include
int main()
{ int a=0,b=1;
char c='A';
printf("a is %d,&a is %p\n",a,&a);//%p格式控制符,表示输出变量a,b,c的地址值。
printf("b is %d,&b is %p\n",b,&b);
printf("c is %d,&c is %p\n",c,&c);
}
//使用指针变量在屏幕上显示变量的地址值
#include
main()
{ int a=0,b=1;
char c='A';
int *pa,*pb;//定义了一个可以指向整型数据的指针变量pa和pb.
char *pc;//定义了可以指向字符型数据的指针变量pc
printf("a is %d,&a is %p,pa is %p\n",a,&a,pa);
printf("b is %d,&b is %p,pb is %p\n",b,&b,pb);
printf("c is %c,&c is %p,pc is %p\n",c,&c,pc);
}
//指针必须赋初值NULL,将上述程序修改如下
#include
main()
{ int a=0,b=1;
char c='A';
int *pa=NULL,*pb=NULL;//定义指针变量并用NULL对其初始化
char *pc=NULL;//定义指针变量并用NULL对其初始化
printf("a is %d ,&a is%p,pa is %p\n",a,&a,pa);
printf("b is %d ,&b is %p,pb is %p\n",b,&b,pb);
printf("cis %c ,&c is %p,pc is %p\n",c,&c,pc);
}
/*但这个结果也不是我们想要的,为了得到正确的运行结果,在指针变量之前必须必须必须将其
指向确定的内存单元。于是,再将程序修改如下*/
#include
main()
{ int a=0,b=1;
char c='A';
int *pa,*pb;/*定义指针变量pa,pb*/
char *pc;/*定义指针变量pc*/
pa=&a;/*初始化指针变量pa,使其指向a*/
pb=&b;/*初始化指针变量pb,使其指向b*/
pc=&c;/* 初始化指针变量pc,使其指向c*/
printf("a is %d ,&a is%p,pa is %p\n",a,&a,pa);
printf("b is %d ,&b is %p,pb is %p\n",b,&b,pb);
printf("cis %c ,&c is %p,pc is %p\n",c,&c,pc);
}