zoukankan      html  css  js  c++  java
  • 27深入理解C指针之---字符串基础

      一、字符串:是以ASCII字符NUL结尾的字符序列,NUL表示为

        1、定义:将字符按顺序存储在数组中,以NUL结尾。

        2、特征:

          1)、每个字符串长度只是包含所有的字符,不包括最后的NUL,手动分配内存是需要加上NUL占用的空间1个字符

          2)、使用字面量直接声明

          3)、使用字符数组声明

          4)、使用字符指针声明

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4
     5 int main(int argc, char **argv)
     6 {
     7     char *str1 = "Sound";
     8     char str2[] = {'S', 'o', 'u', 'n', 'd',};
     9     char *str3 = (char *)malloc(sizeof(char) * 5);
    10     strcpy(str3, "Sound");
    11
    12     printf("*str1: %s
    ", str1);
    13     printf("str2[]: %s
    ", str2);
    14     printf("*str3: %s
    ", str3);
    15
    16     return 0;
    17 }

        以上就是三种声明和赋值方法。


          5)、不同的声明方式,实际上可以是不变的。

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4
     5 int main(int argc, char **argv)
     6 {
     7     char *str1 = "Sound";
     8     char str2[] = {'S', 'o', 'u', 'n', 'd',};
     9     char *str3 = (char *)malloc(sizeof(char) * 5);
    10     strcpy(str3, "Sound");
    11
    12     printf("*str1: %s
    ", str1);
    13     printf("str2[]: %s
    ", str2);
    14     printf("*str3: %s
    ", str3);
    15
    16     //*str1 = 'L';
    17     *str2 = 'L';
    18     *str3 = 'L';
    19     printf("*str1: %s
    ", str1);
    20     printf("str2[]: %s
    ", str2);
    21     printf("*str3: %s
    ", str3);
    22
    23     return 0;
    24 

        在早期的gcc中,第16行是可以的,在gcc 7.2中,只能16行代码是错误的,无法输出正确结果。

        3、其他:

          1)、初始化char数组,声明的同时进行赋值,char arrName[] = "Sound";

          2)、初始化char数组,先声明再赋值,char arrName[];strcpy(arrName,  "Sound");

          3)、初始化char数组,先声明再赋值,char arrName[];arrName[] = {'S', 'o', 'u', 'n', 'd'};

          4)、初始化char数组,先声明再一一赋值,char arrName[];arrName[0] = 'S'; arrName[1] = 'o'; arrName[2] = 'u'; arrName[3] = 'n'; arrName[4] = 'd';

          5)、初始化char指针,声明的同时进行赋值,char *ptrArr = "Sound";

          6)、初始化char指针,先声明,再申请空间,最后赋值,char *ptrArr; ptrArr = (char *)malloc(strlen("Sound") + 1) ;strcpy(ptrArr,  "Sound");

          7)、初始化char指针,先声明再一一赋值,char *ptrArr;*(ptrArr + 0) = 'S'; *(ptrArr + 1) = 'o'; *(ptrArr + 2) = 'u'; *(ptrArr + 3) = 'n'; *(ptrArr + 4) = 'd';


        4、从标准输入初始化字符串:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3
     4 char *getLine(){
     5     const size_t sizeIncrement = 20;                                            //缓冲区初始化是的大小与缓冲区不够时增加的大小
     6     size_t maximumLength = 2 * sizeIncrement;                                   //缓冲区最大的大小,超过此就需要增加缓冲区大小
     7     size_t length = 0;                                                          //已读入的字符数目
     8     char *buffer = malloc(sizeof(char) * sizeIncrement);                        //为缓冲区初始分配的大小
     9     char *currentPosition = buffer;                                             //当前字符的指针
    10     int character;                                                              //当前字符的值
    11
    12     //检验缓冲区是否分配成功
    13     if(currentPosition == NULL){
    14         return NULL;
    15     }
    16
    17     //通过死循环完成不停的输入时,根据需要调整大小,直至输入回车就退出
    18     while(1){
    19         character = fgetc(stdin);                                               //通过标准输入获取当前输入的字符值
    20         if(character == '
    '){                                                  //检验输入的字符是不是结束符
    21             break;
    22         }
    23         //检验输入字符的个数是否超过了缓冲区分配的大小,根据需求实现动态调整
    24         if(++length >= maximumLength){                                          //检验输入字符个数是否达到缓冲区最大值
    25             char *newBuffer = realloc(buffer, maximumLength += sizeIncrement);  //申请新的缓冲区,大小比原来的有所增大
    26
    27             if(newBuffer == NULL){                                              //检验新缓冲区是否分配成功,成功继续,否则需处理
    28                 free(buffer);                                                   //释放先前申请的缓冲区内存
    29                 return NULL;                                                    //退出函数
    30             }
    31             currentPosition = newBuffer + (currentPosition - buffer);           //将当前指针偏置到与原来同样的位置
    32             buffer = newBuffer;                                                 //将老指针指向新分配的内存
    33         }
    34         *currentPosition++ = character;                                         //将新输入的字符存入进缓冲区
    35     }
    36     *currentPosition = '';                                                    //退出输入后,在字符串后添加结束标志
    37
    38     return buffer;                                                              //返回最后的缓冲区的指针
    39 }
    40
    41 int main(int argc, char **argv)
    42 {
    43     char *buffer = getLine();
    44     printf("You input: 
    ");
    45     printf("%s", buffer);
    46
    47     return 0;
    48 }

        程序非常简单,加上注释,阅读应该问题不大,祝好运。



          
          

  • 相关阅读:
    EL
    Cookie & Session
    JSP !
    Request & response
    The use of servlet
    Details about HTTP
    About Tomcat!
    idea提升效率的高频快捷键!(持续更新中)
    Mysql数据库的使用经验总结
    Myeclipse以及Genymotion工具的使用以及java后台开发小结
  • 原文地址:https://www.cnblogs.com/guochaoxxl/p/6960847.html
Copyright © 2011-2022 走看看