6.15 c语言

数组指针

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类型的指针变量称为字符指针变量,字符指针变量与字符有着密切关系,他也被用来处理字符串

初始化字符指针是把内存中字符串的首地址赋予指针

当一个字符指针指向一个字符串常量时,不能修改指针指向的对象的值

c 复制代码
#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";

栈区:指针变量

字符串常量不能被修改,因为存储在静态存储区//会发生段错误

c 复制代码
#include <stdio.h>
#include <stdlib.h>
int main()
{
    char a[] = "hello";
    char *p = "world";
    strcpy(p,a);//段错误,字符串常量被赋值
    puts(a);
    puts(p);
    return 0;
}
c 复制代码
#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;
}
相关推荐
ZCollapsar.1 小时前
数据结构 04(线性:双向链表)
c语言·数据结构·学习·算法·链表
pusue_the_sun1 小时前
C语言强化训练(3)
c语言·开发语言·算法
丑小鸭是白天鹅2 小时前
嵌入式C学习笔记之链表
c语言·笔记·学习
pusue_the_sun8 小时前
C语言强化训练(1)
c语言·开发语言·算法
冷风沐雨12 小时前
LVGL移植(STM32)
c语言·stm32·单片机
小莞尔14 小时前
【51单片机】【protues仿真】基于51单片机音乐喷泉系统
c语言·stm32·单片机·嵌入式硬件·51单片机
胖祥15 小时前
NumPy/PyTorch/C char数组内存排布
c语言·pytorch·numpy
纵有疾風起17 小时前
数据结构——二叉树
c语言·数据结构·算法·链表
小莞尔20 小时前
【51单片机】【protues仿真】基于51单片机智能晾衣架系统
c语言·stm32·单片机·嵌入式硬件·51单片机
蓝风破云21 小时前
模拟实现STL中的list容器
c语言·数据结构·c++·链表·迭代器·list·iterator