c++ basic thoughts: char array

来源:互联网 发布:人工智能龙头股票 编辑:程序博客网 时间:2024/06/05 05:24
//strtype3.cpp .. more string class features
#include <iostream>
#include <string>
#include <cstring>          // C-styple string library

int main()
{
    using namespace std;
    char charr1[20];
    char charr2[20] = "jaguar";
    string str1;
    string str2 = "panther";

    // assignment for string objects and character arrays
    str1 = str2;
    strcpy(charr1,charr2);

    //appending for string objects and character arrays
    str1 += " paste";
    strcat(charr1, " juice");

    // finding the length of a string object and a C-style string
    int len1 = str1.size();      // obtain length of str1
    int len2 = strlen(charr1);  // obtain length of charr1

    cout << "The string " << str1 << " contains " << len1 << " characters.\n";
    cout << "The string " << charr1 << " contains " << len2 << " characters.\n";

    char site[6]={};
    cout << strcat(site, " of pancakes") << endl;;


    return 0;

}

Question & answer:

The definition of  char site[6] means 'site' gets a address (definitly saying I don't know why?maybe i can answer it  on some day) if you use the expresson: "char site[];" replace the "char site[6] = {};" what do you got? A strange symboll in your monitor. the further more we know we should make sure that 'site[6]' need get the guaranteed value, the initial value.different compiler maybe have different behavior. 

原创粉丝点击