6.15 c语言

数组指针

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a[3][2] = {{1,2},{3,4},{5,6}};
    int (*p)[2],i,j;
    p = a;
    for(i=0;i<3;i++)
    {
        for(j=0;j<2;j++)
        {
            printf("%d %d\n",p[i][j],*(*(p+i)+j));
        }
        printf("\n");
    }
    return 0;
}

10.7 字符指针和字符串

c语言通过使用字符数组来处理字符串
char类型的指针变量称为字符指针变量,字符指针变量与字符有着密切关系,他也被用来处理字符串
初始化字符指针是把内存中字符串的首地址赋予指针
当一个字符指针指向一个字符串常量时,不能修改指针指向的对象的值

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char ch1[] = "hello";
    char ch2[] = "hello";
    char *p;
    p = ch1;
    if(isalpha(*p))
    {
        if(isupper(*p))
        {
            *p = tolower(*p);
        }
        else
        {
            *p = toupper(*p);
        }
    }
    printf("%s %s\n",p,ch1);
    p = ch2;
    printf("%s\n",ch2);
    return 0;
}

静态存储区:
1、全局变量
2、static局部变量
3、字符串常量//char *p = “welcome”;
栈区:指针变量
字符串常量不能被修改,因为存储在静态存储区//会发生段错误

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char a[] = "hello";
    char *p = "world";
    strcpy(p,a);//段错误,字符串常量被赋值
    puts(a);
    puts(p);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>//实现字符串连接功能
int main()
{
    char a[100] = "hello world";
    char *p = "welcome";
    int i = 0;
    char *q;
    q = p;
    while(*(a+i) != '\0')
    {
        i++;
    }
    while(*p != '\0')
    {
        *(a+i) = *p;
        p++;
        i++;
    }
    *(a+i) = *p;
    p = q;
    puts(a);
    puts(p);
    return 0;
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值