十三、C指针详解(三):字符串指针

一、 字符串的定义与遍历

字符串其实就是字符数组,归根结底是一个数组

  • 代码实现:
    #include<stdio.h>
    #include<string.h>
    
    void main(){
        char str[] = "hello world!";
        int len = strlen(str);//计算字符串长度,不包括末尾的\0
        int size = sizeof(str);//计算字符串的长度,双引号之间的都包括,包括\0
        printf("字符串长度(strlen):%d\n",len);
        printf("字符串长度(sizeof):%d\n",size);
        printf("每次输出一个字符:");
        for(int i = 0; i < len; i++){
            printf("%c",str[i]);
        }
        printf("\n");
        printf("直接输出字符串:%s\n\n",str);
        //在C语言中,通过判断末尾的\0,来判断字符串是否结束
        char str1[] = "hello worl\0d!";
        int len1 = strlen(str1);//计算字符串长度,不包括末尾的\0
        int size1 = sizeof(str1);//计算字符串的长度,双引号之间的都包括,包括\0
        printf("字符串长度(strlen):%d\n",len1);
        printf("字符串长度(sizeof):%d\n",size1);
        printf("每次输出一个字符:\n");
        printf("通过len1:");
        for(int i = 0; i < len1; i++){
            printf("%c",str1[i]);
        }
        printf("\n");
        printf("通过size1:");
        for(int i = 0; i < size1; i++){
            printf("%c",str1[i]);
        }
        printf("\n");
        printf("直接输出字符串:%s\n",str1);
    }
    
  • 运行结果:
    d102@d102-W65KJ1-KK1:/media/d102/EPAN/Desktop/c++_ubuntu/c++_01/workspace$ gcc test2.c -o t2.o
    d102@d102-W65KJ1-KK1:/media/d102/EPAN/Desktop/c++_ubuntu/c++_01/workspace$ ./t2.o 
    字符串长度(strlen):12
    字符串长度(sizeof):13
    每次输出一个字符:hello world!
    直接输出字符串:hello world!
    
    字符串长度(strlen):10
    字符串长度(sizeof):14
    每次输出一个字符:
    通过len1:hello worl
    通过size1:hello world!
    直接输出字符串:hello worl
    

二、使用指针输出字符数组

  • 代码实现:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char str[] = "hello world";
        //直接指向字符串的指针
        char *str_p = "hello world";
        char *p = str; //存放字符串首个元素的地址,str代表数组首个元素的地址
        printf("指针的内存大小:%ld\n", sizeof(str_p));
    
        printf("str_p:%s\n", str_p);
        printf("str:%s\n", str);
    
        printf("str_p首地址:%p\n", str_p);
        printf("str首地址:%p\n", str);
    
        printf("str_p str_p[4]:%c\n", str_p[4]);
        printf("str str[4]:%c\n", str[4]);
    
        printf("str_p str_p[4]:%c\n", *(str_p + 4));
        printf("str str[4]:%c\n", *(str + 4));
        
        int len = strlen(str);
        // 直接打印,在printf函数中,%s格式的输出
        // 当遇到\0,即认为字符串结束,才会停止打印
        
        //单个字符打印,*(p+i)
        printf("单个字符打印 *(p+i) :");
        for (int i = 0; i < len; i++)
        {
            printf("%c", *(p + i));
        }
        printf("\n");
    
        //单个字符打印,*(str+i)
        printf("单个字符打印 *(str+i):");
        for (int i = 0; i < len; i++)
        {
            printf("%c", *(str + i));
        }
        printf("\n");
                
        //使用p[i]
        printf("使用p[i]打印:");
        for (int i = 0; i < len; i++)
        {
            printf("%c", p[i]);
        }
        printf("\n");
    
        return 0;
    }
    
  • 运行结果:
    指针的内存大小:8
    str_p:hello world
    str:hello world
    str_p首地址:0x563c2f0bb008
    str首地址:0x7ffdf7a46fdc
    str_p str_p[4]:o
    str str[4]:o
    str_p str_p[4]:o
    str str[4]:o
    单个字符打印 *(p+i) :hello world
    单个字符打印 *(str+i):hello world
    使用p[i]打印:hello world
    

三、字符数组(字符串变量)与字符指针(字符串常量)

3.1 区别与联系

从第二结可以看出,字符数组和字符指针在使用上没有任何区别,都是字符串的两种表示形式,但是它们并不完全一样。字符数组是可以读写的,而字符指针只可以读,不可以写

  • 字符数组形式:
    char str[] = "hello world";
    
  • 字符指针形式:
    char *str = "hello world";
    
  • 代码实现:
    #include<stdio.h>
    
    int main(){
    
        char str[] = "hello world";
        str[0] = 'x';
        str[1] = 'x';
        printf("str:%s\n",str);
    
        char *str_p = "hello world";
        str_p = "xiao ma ge";
        // str_p[0] = 'x'; //报错:Segmentation fault
        printf("str_p:%s\n",str_p);
    
    }
    
  • 运行结果:
    str:xxllo world
    str_p:xiao ma ge
    

3.2 存储方式

  • 字符数组由若干元素组成,每个元素存放一个字符
  • 字符指针只存放字符串的首地址,不是整个字符串

3.3 赋值方式

  • 字符数组下面的赋值方式错误,只能一个元素一个元素赋值:
    char str[20];
    str = "hello";
    
  • 字符指针可以采用下面的赋值方式:
    char *str;
    str = "hello";
    

3.4 可否被修改

字符指针可以改变其指向的内容,也可以改变其指向,但是不能更改其指向内容里的某一元素

  • 代码实现:
    #include<stdio.h>
    
    int main(){
    
        char str[] = "hello world";
        str[0] = 'x';
        str[1] = 'x';
        printf("str:%s\n",str);
    
        char *str_p = "hello world";
        str_p = "xiao ma ge";//改变指向内容
        // str_p[0] = 'x'; //报错:Segmentation fault
        printf("str_p:%s\n",str_p);
    
    
        char c[] = "abc";
        str_p = c;//改变其指向
        printf("str_p:%s\n",str_p);
    }
    
  • 运行结果:
    str:xxllo world
    str_p:xiao ma ge
    str_p:abc
    

3.5 初始化

  • 字符数组:
    char str[10];
    scanf("%s",str);
    
  • 字符指针:
    char *p = NULL;//防止出现野指针
    p = (char *)malloc(10);
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值