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;
}
相关推荐
JAY_LIN——83 小时前
字符串函数(strncpy/cat/cmp、strstr、strtok、strerror)
c语言·开发语言
不爱吃糖的程序媛3 小时前
鸿蒙PC端运行C语言程序:从编译到部署的全流程实战
c语言·华为·harmonyos
历程里程碑3 小时前
滑动窗口秒解LeetCode字母异位词
java·c语言·开发语言·数据结构·c++·算法·leetcode
Tandy12356_4 小时前
手写TCP/IP协议栈——TCP结构定义与基本接口实现
c语言·网络·c++·网络协议·tcp/ip·计算机网络
benjiangliu4 小时前
STM32教程-02-STM32复习C语言
c语言·stm32·嵌入式硬件
潇氡4 小时前
C语言“指针变量“在初始化和做函数参数时的注意事项
c语言
程芯带你刷C语言简单算法题5 小时前
Day39~实现一个算法确定将一个二进制整数翻转为另一个二进制整数,需要翻转的位数
c语言·开发语言·学习·算法·c
永远前进不waiting5 小时前
C语言复习——2
c语言·开发语言
白书宇6 小时前
【STM32实战】从零开始写Linux 0.12内核 第1个实验安装IAR 8.5
linux·c语言·驱动开发·stm32·单片机·嵌入式硬件
lengjingzju6 小时前
一网打尽Linux IPC(四):POSIX IPC
linux·服务器·c语言