• 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;
    }
    
  • 相关阅读:
    Warning: Cannot modify header information
    curl_setopt(): CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in
    PHP Warning: strtotime(): It is not safe to rely on the system's timezone settings.
    出现Deprecated: Function ereg_replace() is deprecated in 的原因及解决方法
    APMServ5.2.6 升级PHP版本 到高版本 5.3,5.4
    Apache无法启动解决 the requested operation has failed
    用json获取拉钩网的信息
    json
    如何用组件组装一个简单的机器人
    vue-组件
  • 原文地址:https://www.cnblogs.com/aiti/p/4653645.html
Copyright © 2020-2023  润新知