字符指针、字符串、字符数组、字符串数组等

参考:https://xiefor100.blog.csdn.net/article/details/52667734

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char s1[] = "12345";   // "12345"在栈区,可以指针偏移读取和修改
    char *s2 = "12345";    // "12345"在常量区,可以指针偏移读取,但是不能修改
    char *ptr[]={"xxxxxxxx","12345678","c8765432"};  //不能定义为char **ptr,因为这个是表示一个指向指针的指针,但在这里它被赋值为一个字符串常量数组,这会导致类型不匹配的错误。
    printf("s1[] size : %d\n", sizeof(s1));    //6  数组的大小
    printf("修改前s1[0]  : %c\n", s1[0]);  //0
    s1[0] = '6';
    printf("修改后s1[0]  : %c\n", s1[0]);  //6
    *(s1 + 1) = '0';
    printf("修改后s1[1]  : %c\n", s1[1]);  //0
    printf("修改后s1[]数组  : %s\n\n", s1);

    printf("*s2 size : %d\n", sizeof(s2));     //8  指针的大小
    printf("s2 value  : %c\n\n", *(s2+1));   //2
    //*(s2+1) = 6;   //出错,不能修改

    printf("ptr size : %d\n", sizeof(ptr));   //24
    printf("*ptr size : %d\n", sizeof(*ptr));  //8
    printf("ptr[1] value : %s\n", ptr[1]);     //12345678
    printf("ptr[1]+1 value : %s\n", ptr[1]+1);      //第二个子字符串,偏移1位,即2345678
    printf("*(&ptr[1]+1) value : %s\n", *(&ptr[1]+1));  //c8765432
    printf("*(&ptr[1]+1)+1 value : %s\n", *(&ptr[1]+1)+1);  //第三个子字符串,偏移1位,即8765432

    return 0;
}

在这里插入图片描述
具体可以参考这篇文章:https://blog.csdn.net/m0_46606290/article/details/119787885

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值