码迷,mamicode.com
首页 > 编程语言 > 详细

c语言字符串指针

时间:2016-07-19 18:34:37      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

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

  字符串指针

 1 void main()
 2 {
 3     char *p = "tasklist";       //指针,字符串指针
 4     printf("%d", sizeof(p));
 5     printf("\n%d", sizeof("tasklist")); //9个字符   tasklist \0
 6     //p存储的是常量字符串 "tasklist"的首地址,即t字符的地址
 7     //*p = ‘1‘ //无法赋值, tasklist是指针,指针是常量无法赋值。
 8 
 9     printf("\n%c",*p);      //t
10     printf("\n%c",*(p+1));  //a
11 
12     getchar();
13 }

  字符串指针数组

 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 }

 

c语言字符串指针

标签:

原文地址:http://www.cnblogs.com/loveyouyou616/p/5685641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!