• 0032 字符串指针


    /*
     
        字符串指针:
     
           定义: char *字符串指针变量名 = "字符串内容";
     
           用作:用来保存一个字符串
     
     
     
     
     
     
     */
    
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(int argc, const char * argv[]) {
       
        //"zfbanzhang zaiyiqi" 这个字符串存储在常量去
        //str 只保存了字符串常量的首地址
        char *str = "zfbanzhang zaiyiqi";
        //指针变量都是占用8个字节
        printf("sizeof(str) = %ld
    ",sizeof(str));
        printf("str len = %ld
    ",strlen(str));
        
        //str是指针变量
        //str重新指向另外一个字符串常量 "I LOVE YOU"
        //保存的是 "I LOVE YOU" 字符串常量的首地址
        str = "I LOVE YOU";
        printf("%s
    ",str);
        
        char ch[]="abc";  //存在栈区
        ch[2]='Z';
        printf("%s
    ",ch);
        
        //读取字符串常量的某个字符
        printf("%c
    ",*(str+2));  //L
        for (int i=0; i<strlen(str); i++) {
            printf("%c	",*(str+i));
        }
        
        //
        //*(str+2)='X';  错误的,因为字符串常量是在常量区存储
        //在常量区保存的数据都是只读的
        
        //str2 没有赋初值,野指针
        char *str2=NULL;
        //这句话的作用给先申请了100个字节的内存 str2
        str2 = malloc(100);
        
        
        printf("=======%s
    ",str2);
        
        //另外一种解决方案
        char ch3[100];
        char *str3=ch3;
        scanf("%s",str3);
        printf("----->%s
    ",str3);
        
        return 0;
    }
    
    /*
     
        一维字符数组
     
           char  ch[10]={'a','b'};
     
           char ch1[]="abc";
     
     
        二维字符数组
     
           char ch2[3][10]={{'a','b'},{'b'},{'c'}};
     
        用二维数组来保存多个字符串
     
           //用二维的字符数组可以存储多个字符串
           //第一维存的是每个字符串的首地址
           //每个字符串的长度,不能超过第二维长度
           char ch3[3][5]={"abc","def","kkkk"}
      
     
               a  b  c    
               d  e  f    
               k  k  k  k   
     
     
               ch[1][3] = 'Z';
     
    
    
    
    
     */
    
    
    #include <stdio.h>
    
    int main(int argc, const char * argv[]) {
        
        char ch3[3][5]={"abc","def","kkkk"};
        //存放的是一个二维数组
        ch3[1][3] = 'Z';
        
        for (int i=0; i<3; i++) {
            printf("%s
    ",ch3[i]);
        }
        
        return 0;
    }
    
    //
    //  main.c
    //  18、char型指针数组和字符数组区别
    //
    //  Created by apple on 15/1/8.
    //  Copyright (c) 2015年 itcast. All rights reserved.
    //
    
    #include <stdio.h>
    
    int main(int argc, const char * argv[]) {
       
        //char 类型的指针数组
        char *name[3]={"abcdasfadsfasdfasdfasdf","def","kkk"};
        
        for (int i=0; i<3; i++) {
            //name[i]
            printf("%s
    ",*(name+i));
        }
        
        //字符串指针和字符数组的区别
        
        //字符串指针:
                          char *ss = "abc";
        
        //指向可以改变
        //ss是一个指针变量
                          ss = "helloWorld!";
        
        //字符数组:
        
                          char s1[]="abc";
        //s1是数组名,是一个常量,不能被复制
                          //s1 = "helloworld!";
        
        return 0;
    }
    
    //
    //  main.c
    //  19-应用:字符串排序
    //
    //  Created by apple on 15/1/8.
    //  Copyright (c) 2015年 itcast. All rights reserved.
    //
    
    #include <stdio.h>
    #include <string.h>
    
    /**
     *  实现字符串的排序
     *
     *  @param arr <#arr description#>
     *  @param len <#len description#>
     */
    void sortString(char *arr[],int len){
    
         //冒泡排序
         //临时存放地址
        char *temp;
        for (int i=0; i<len-1; i++) {
            for (int j=0; j<len-i-1; j++) {
                
                //arr[j]  arr[j+1]
                //比较字符串大小
                if (strcmp(arr[j], arr[j+1])>0) {
                    //实现arr[j]  arr[j+1]交换
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                    
                }
                
            }
        }
        
    
    }
    
    int main(int argc, const char * argv[]) {
        
        //输入5个国名并按字母顺序排列后输出。
        char *name[]={ "CHINA","AMERICA","AUSTRALIA","FRANCE","GERMAN"};
        
        for (int i=0; i<5; i++) {
            printf("%s
    ",name[i]);
        }
        
        //字符串排序
        sortString(name, 5);
        
        printf("
    
    ");
        for (int i=0; i<5; i++) {
            printf("%s
    ",name[i]);
        }
        
        
        
        int a[2][3]={1,2,3,4,5,6};
        //p->0x01 1
        //   0x02 2
        //   0x03 3
        //   0x04 4
        // a = &a[0] = &a[0][0]
    
        
        return 0;
    }
    
  • 相关阅读:
    练习 : Flink 自定义 Source & Sink to Redis
    SQL 练习
    HiveSQL 常用函数说明
    Java 开发岗
    练习 : Flink 自定义 sink to hbase
    YOLOv6: a singlestage object detection framework dedicated to industrial applications.
    YOLO with Transformers and Instance Segmentation, with TensorRT acceleration
    About A paper list of some recent Transformerbased CV works
    This is an official implementation for "Swin Transformer: Hierarchical Vision Transformer using Shifted Windows".
    Coming from computer vision and new to transformers? Here are some resources that greatly accelerated my learning.
  • 原文地址:https://www.cnblogs.com/aiti/p/4653645.html
Copyright © 2020-2023  润新知