• nullnullC++ LANGUAGE TUTORIAL: CHARACTER ARRAYS


    文章结束给大家来个程序员笑话:[M]

        

    CHARACTER ARRAYS

    #include "stdio.h"
    
    int main()
    {
    	int a[10]={0,1,2,3,4,5,6,7,8,9};
    	char b[6]={'H','E','L','L','O','\0'};
    	printf("sizeof INT is: %d\n",sizeof(int));
    	int i=0;
    	for(i=0;i<10;i++)
    		printf("a[%d]=%d, &a[%d]=%d\n",i,a[i],i,&a[i]);
    		
    	printf("sizeof CHAR is: %d\n",sizeof(char));
    	
    	for(i=0;i<6;i++)
    	{
    		//printf("b[%d]=%d, &b[%d]=%d\n",i,b[i],i,&b[i]);
    		printf("b[%d]=%c, &b[%d]=%d\n",i,b[i],i,&b[i]);
    	}
    	printf("\n");
    		for(i=0;i<6;i++)
    	{
    		printf("b[%d]=%d, &b[%d]=%d\n",i,b[i],i,&b[i]);
    		//printf("b[%d]=%c, &b[%d]=%d\n",i,b[i],i,&b[i]);
    	}
    		
    	return 0;
    }
    
    /**
    OCS101:~/cpl # gcc testArray.c 
    OCS101:~/cpl # ./a.out 
    sizeof INT is: 4
    a[0]=0, &a[0]=-1857442672
    a[1]=1, &a[1]=-1857442668
    a[2]=2, &a[2]=-1857442664
    a[3]=3, &a[3]=-1857442660
    a[4]=4, &a[4]=-1857442656
    a[5]=5, &a[5]=-1857442652
    a[6]=6, &a[6]=-1857442648
    a[7]=7, &a[7]=-1857442644
    a[8]=8, &a[8]=-1857442640
    a[9]=9, &a[9]=-1857442636
    sizeof CHAR is: 1
    b[0]=H, &b[0]=-1857442688
    b[1]=E, &b[1]=-1857442687
    b[2]=L, &b[2]=-1857442686
    b[3]=L, &b[3]=-1857442685
    b[4]=O, &b[4]=-1857442684
    b[5]=, &b[5]=-1857442683
    
    b[0]=72, &b[0]=-1857442688
    b[1]=69, &b[1]=-1857442687
    b[2]=76, &b[2]=-1857442686
    b[3]=76, &b[3]=-1857442685
    b[4]=79, &b[4]=-1857442684
    b[5]=0, &b[5]=-1857442683
    OCS101:~/cpl # ./a.out 
    sizeof INT is: 4
    a[0]=0, &a[0]=1756439296
    a[1]=1, &a[1]=1756439300
    a[2]=2, &a[2]=1756439304
    a[3]=3, &a[3]=1756439308
    a[4]=4, &a[4]=1756439312
    a[5]=5, &a[5]=1756439316
    a[6]=6, &a[6]=1756439320
    a[7]=7, &a[7]=1756439324
    a[8]=8, &a[8]=1756439328
    a[9]=9, &a[9]=1756439332
    sizeof CHAR is: 1
    b[0]=H, &b[0]=1756439280
    b[1]=E, &b[1]=1756439281
    b[2]=L, &b[2]=1756439282
    b[3]=L, &b[3]=1756439283
    b[4]=O, &b[4]=1756439284
    b[5]=, &b[5]=1756439285
    
    b[0]=72, &b[0]=1756439280
    b[1]=69, &b[1]=1756439281
    b[2]=76, &b[2]=1756439282
    b[3]=76, &b[3]=1756439283
    b[4]=79, &b[4]=1756439284
    b[5]=0, &b[5]=1756439285
    OCS101:~/cpl # ./a.out 
    sizeof INT is: 4
    a[0]=0, &a[0]=370342176
    a[1]=1, &a[1]=370342180
    a[2]=2, &a[2]=370342184
    a[3]=3, &a[3]=370342188
    a[4]=4, &a[4]=370342192
    a[5]=5, &a[5]=370342196
    a[6]=6, &a[6]=370342200
    a[7]=7, &a[7]=370342204
    a[8]=8, &a[8]=370342208
    a[9]=9, &a[9]=370342212
    sizeof CHAR is: 1
    b[0]=H, &b[0]=370342160
    b[1]=E, &b[1]=370342161
    b[2]=L, &b[2]=370342162
    b[3]=L, &b[3]=370342163
    b[4]=O, &b[4]=370342164
    b[5]=, &b[5]=370342165
    
    b[0]=72, &b[0]=370342160
    b[1]=69, &b[1]=370342161
    b[2]=76, &b[2]=370342162
    b[3]=76, &b[3]=370342163
    b[4]=79, &b[4]=370342164
    b[5]=0, &b[5]=370342165
    
    */

        
     

        As you may already know, the C++ Standard Library implements a powerful

         string

        class, which is very useful to handle and manipulate strings of characters. However, because strings are in fact sequences of characters, we can represent them also as plain arrays of

        

        char

        

        elements.

        For example, the following array:

     
    char jenny [20];

    is an array that can store up to 20 elements of type

        

        char

        

        . It can be represented as:

        null和null

        Therefore, in this array, in theory, we can store sequences of characters up to 20 characters long. But we can also store shorter sequences. For example,

        

        jenny

        

        could store at some point in a program either the sequence

        

        "Hello"

        

        or the sequence

        

        "Merry christmas"

        

        , since both are shorter than 20 characters.

        Therefore, since the array of characters can store shorter sequences than its total length, a special character is used to signal the end of the valid sequence: the

        

        null character

        

        , whose literal constant can be written as

        

        '\0'

        

        (backslash, zero).

        Our array of 20 elements of type

        

        char

        

        , called

        

        jenny

        

        , can be represented storing the characters sequences

        

        "Hello"

        

        and

        

        "Merry Christmas"

        

        as:

        null和null

        Notice how after the valid content a null character (

        

        '\0'

        

        ) has been included in order to indicate the end of the sequence. The panels in gray color represent

        

        char

        

        elements with undetermined values.

        

    Initialization of null-terminated character sequences

        Because arrays of characters are ordinary arrays they follow all their same rules. For example, if we want to initialize an array of characters with some predetermined sequence of characters we can do it just like any other array:

     
    char myword[] = { 'H', 'e', 'l', 'l', 'o', '\0' }; 

    In this case we would have declared an array of 6 elements of type

        

        char

        

        initialized with the characters that form the word

        

        "Hello"

        

        plus a null character

        

        '\0'

        

        at the end.

        But arrays of

        

        每日一道理
    书籍好比一架梯子,它能引领人们登上文化的殿堂;书籍如同一把钥匙,它将帮助我们开启心灵的智慧之窗;书籍犹如一条小船,它会载着我们驶向知识的海洋。

        char

        

        elements have an additional method to initialize their values: using string literals.

        In the expressions we have used in some examples in previous chapters, constants that represent entire strings of characters have already showed up several times. These are specified enclosing the text to become a string literal between double quotes ("). For example:

     
    "the result is: "

    is a constant string literal that we have probably used already.

        Double quoted strings (

        

        "

        

        ) are literal constants whose type is in fact a null-terminated array of characters. So string literals enclosed between double quotes always have a null character (

        

        '\0'

        

        ) automatically appended at the end.

        Therefore we can initialize the array of

        

        char

        

        elements called

        

        myword

        

        with a null-terminated sequence of characters by either one of these two methods:

    1
    2
    
    char myword [] = { 'H', 'e', 'l', 'l', 'o', '\0' };
    char myword [] = "Hello"; 

    In both cases the array of characters

        

        myword

        

        is declared with a size of 6 elements of type

        

        char

        

        : the 5 characters that compose the word

        

        "Hello"

        

        plus a final null character (

        

        '\0'

        

        ) which specifies the end of the sequence and that, in the second case, when using double quotes (

        

        "

        

        ) it is appended automatically.

        Please notice that we are talking about initializing an array of characters in the moment it is being declared, and not about assigning values to them once they have already been declared. In fact because this type of null-terminated arrays of characters are regular arrays we have the same restrictions that we have with any other array, so we are not able to copy blocks of data with an assignment operation.

        Assuming

        

        mystext

        

        is a

        

        char[]

        

        variable, expressions within a source code like:

    1
    2
    
    mystext = "Hello";
    mystext[] = "Hello"; 

    would

        

        not

        

        be valid, like neither would be:

     
    mystext = { 'H', 'e', 'l', 'l', 'o', '\0' };

    The reason for this may become more comprehensible once you know a bit more about pointers, since then it will be clarified that an array is in fact a constant pointer pointing to a block of memory.

        

    Using null-terminated sequences of characters

        Null-terminated sequences of characters are the natural way of treating strings in C++, so they can be used as such in many procedures. In fact, regular string literals have this type (

        

        char[]

        

        ) and can also be used in most cases.

        For example,

        

        cin

        

        and

        

        cout

        

        support null-terminated sequences as valid containers for sequences of characters, so they can be used directly to extract strings of characters from

        

        cin

        

        or to insert them into

        

        cout

        

        . For example:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    // null-terminated sequences of characters
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      char question[] = "Please, enter your first name: ";
      char greeting[] = "Hello, ";
      char yourname [80];
      cout << question;
      cin >> yourname;
      cout << greeting << yourname << "!";
      return 0;
    }
    Please, enter your first name: John
    Hello, John!

    As you can see, we have declared three arrays of

        

        char

        

        elements. The first two were initialized with string literal constants, while the third one was left uninitialized. In any case, we have to specify the size of the array: in the first two (

        

        question

        

        and

        

        greeting

        

        ) the size was implicitly defined by the length of the literal constant they were initialized to. While for

        

        yourname

        

        we have explicitly specified that it has a size of 80 chars.

        Finally, sequences of characters stored in

        

        char

        

        arrays can easily be converted into

        

        string

        

        objects just by using the assignment operator:

    1
    2
    3
    
    string mystring;
    char myntcs[]="some text";
    mystring = myntcs;

    文章结束给大家分享下程序员的一些笑话语录: 小沈阳版程序员~~~ \n程序员其实可痛苦的了......需求一做一改,一个月就过去了;嚎~ \n需求再一改一调,一季度就过去了;嚎~ \n程序员最痛苦的事儿是啥,知道不?就是,程序没做完,需求又改了; \n程序员最最痛苦的事儿是啥,知道不? 就是,系统好不容易做完了,方案全改了; \n程序员最最最痛苦的事儿是啥,知道不? 就是,系统做完了,狗日的客户跑了; \n程序员最最最最最痛苦的事儿是啥,知道不? 就是,狗日的客户又回来了,程序给删没了!

  • 相关阅读:
    九、linux-msyql下的mysql主从复制深度实战
    八、linux-mysql的mysql主从复制原理和实战
    七、linux-mysql下mysql增量备份与恢复
    六、linux-mysql的mysql字符集问题
    入坑django2
    python根据已有数据库生成model.py
    docker 常用命令记录
    gitlab自带的Nginx与原Nginx冲突的解决方案
    linux下PHP安装redis扩展
    jenkins自动部署代码到多台服务器
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3078471.html
Copyright © 2020-2023  润新知