• c语言字符串指针


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

      字符串指针  

     1 void main()
     2 {
     3     char *p = "tasklist";
     4 
     5     printf("%d
    ", sizeof(p)); //4 ,指针4个字节
     6     printf("%d
    ", sizeof("tasklist")); //9个字符   tasklist
     7     printf("%d
    ", sizeof(*p));//1
     8 
     9     //p存储的是常量字符串 "tasklist"的首地址,即t字符的地址
    10     //*p = '1' //无法赋值, tasklist是指针,指针是常量无法赋值。
    11 
    12     printf("%s
    ",p); //tasklist  p是指针变量的首地址
    13     printf("%c
    ",*p);//t         所以取内容 *p => 首地址的值 t
    14     printf("%c
    ",*(p+1));//a     同上
    15     printf("%c
    ",*(p+2));//s     同上
    16 
    18     //printf("%x
    ",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
    ",*(p[i]));  // 打印出字符t
    13         printf("%x,%s
    ",p[i],p[i]);
    14     }
    15 
    16     system("pause");
    17 
    18 }
  • 相关阅读:
    css3 3d 转换
    css3 动画序列
    css3 动画
    2d 转换之缩放
    2d 转换中心点
    css3 书写 动画三角形
    2d 旋转
    2D转换
    伪元素 字体图标
    风陵01
  • 原文地址:https://www.cnblogs.com/loveyouyou616/p/5685641.html
Copyright © 2020-2023  润新知