最近正在看c语言,在指针这块遇到了麻烦,特别是字符串指针这块,简单记录下。

  字符串指针  

 1 void main()
 2 {
 3     char *p = "tasklist";
 4 
 5     printf("%d\n", sizeof(p)); //4 ,指针4个字节
 6     printf("%d\n", sizeof("tasklist")); //9个字符   tasklist\0
 7     printf("%d\n", sizeof(*p));//1
 8 
 9     //p存储的是常量字符串 "tasklist"的首地址,即t字符的地址
10     //*p = '1' //无法赋值, tasklist是指针,指针是常量无法赋值。
11 
12     printf("%s\n",p); //tasklist  p是指针变量的首地址
13     printf("%c\n",*p);//t         所以取内容 *p => 首地址的值 t
14     printf("%c\n",*(p+1));//a     同上
15     printf("%c\n",*(p+2));//s     同上
16 
18     //printf("%x\n",p);
19     int *s = p;      //将首地址 赋给 指针变量s
20     printf("%c",*s); //t ,即取地址值
21 
22 }
  字符串指针数组
 1 //字符串指针数组
 2 void main2()
 3 {
 4     //指针数组p 存储的元素是指针类型,即top,ll,ls都为指针类型(常量无法赋值)
 5     char *p[] = {"top","ll","ls"};
 6     int l = sizeof(p) / sizeof(char *);
 7     //printf("%d", sizeof(p) / sizeof(char *));//求数组多少元素
 8     int i = 0;
 9 
10     for (;i < l; i++) {
11         //i=1为例 , p[i]为top 指向 top的首地址。即t字符的地址
12         printf("%c\n",*(p[i]));  // 打印出字符t
13         printf("%x,%s\n",p[i],p[i]);
14     }
15 
16     system("pause");
17 
18 }

 

posted on 2016-07-19 17:30  睡着的糖葫芦  阅读(330)  评论(0编辑  收藏  举报