简体   繁体   English

C ++中的char []和char [10]有什么区别?

[英]What's the difference between char[] and char[10] in C++?

Is there any difference between char[] and char[10] (or any other arbitrary constant)? char []和char [10](或任何其他任意常数)之间有什么区别吗?

for example: 例如:

char[] = "here";
char[10] = "there";

When I ran such a program: 当我运行这样的程序时:

struct TreeNode
{
    struct TreeNode* left;
    struct TreeNode* right;
    char elem;
};
void BinaryTreeFromOrderings(char* ,char* ,int);

int main()
{
    char a[] = "";
    char b[] = "";
    cin >> a >> b;
    BinaryTreeFromOrderings(b, a, strlen(a));
    return 0;
}

void BinaryTreeFromOrderings(char* inorder, char* preorder, int length)
{
    if(length == 0) return;
    TreeNode* node = new TreeNode;
    node->elem = *preorder;
    int rootIndex = 0;
    for(;rootIndex < length ; rootIndex ++)
    {
        if(inorder[rootIndex] == *preorder)
            break;
    }
    //left
    BinaryTreeFromOrderings(inorder,preorder+1,rootIndex);
    //right
    BinaryTreeFromOrderings(inorder + rootIndex +1 ,preorder + rootIndex +1,length - (rootIndex + 1));
    cout << node->elem;
    delete [] node;
    return;
}

The result seems right, but the program will dump just before exit. 结果似乎正确,但是程序将在退出前转储。

Then I made an experiment: 然后我做了一个实验:

int main()
{
    char a[] = "";
    cin >> a;
    cout << a;
    return 0;
}

It will run successfully when I input less than 9 characters. 当我输入少于9个字符时,它将成功运行。 (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)) (gcc版本4.6.3(Ubuntu / Linaro 4.6.3-1ubuntu5))

And if i initialize a[] with: 如果我用以下方法初始化a []:

char a[] = "123456789";

It will success less than 25 charactres. 它将成功少于25个字符。

I guess there's something that compiler cannot determine the size of a. 我猜有些东西编译器无法确定a的大小。 But what's the detailed reasons? 但是,具体原因是什么?

 char[] = "here";

This is an array is size 5, automatically deduced from the 4 letters, plus an implicit null terminator ('\\0') tacked onto the end. 这是一个大小为5的数组,该数组是从4个字母中自动推导出来的,最后加上一个隐式空终止符('\\ 0')。 You are allowed to write and read from positions 0-4. 允许您从位置0-4进行写入和读取。 Anything else is undefined behavior. 其他都是未定义的行为。

char[10] = "there";

This is an array is size 10, contents "there\\0\\0\\0\\0\\0" . 这是一个大小为10的数组,内容为"there\\0\\0\\0\\0\\0" You are allowed to write and read from positions 0-9. 允许您从0-9位置写入和读取。 Anything else is undefined behavior. 其他都是未定义的行为。

char a[] = "";

This is an array of size 1, just a null terminator. 这是一个大小为1的数组,只是一个空终止符。 When you input 9 characters into it, that's undefined behavior. 当您在其中输入9个字符时,这是未定义的行为。 (actually, using standard string input functions, you can't even safely input 1 character, because the standard string input functions automatically tack on a null terminator. (实际上,使用标准字符串输入函数,您甚至不能安全地输入1个字符,因为标准字符串输入函数会自动添加到空终止符上。

char a[] = "123456789";

This is an array of size 10, and when you input 25 characters into it, that's undefined behavior. 这是一个大小为10的数组,当您在其中输入25个字符时,这是未定义的行为。

http://en.wikipedia.org/wiki/Undefined_behavior http://en.wikipedia.org/wiki/Undefined_behavior

char a[] = "here";

The compiler will determine the size of the char array a , which is 4 characters + 1 ending character \\0 . 编译器将确定char数组a的大小,即4个字符+ 1个结束字符\\0

char a[10] = "there";

The size of char array a is 10 including the \\0 , so you can put at most 9 chars into int. 字符数组的大小a是10包括\\0 ,所以可以把至多 9个字符转换成int。 Otherwise, you are writing to memory that does not belong to the array. 否则,您正在写入不属于该阵列的内存。 If you do the above way, character 5-9 are null initialized. 如果执行上述方法,则将字符5-9初始化为空。 See a live example here: http://ideone.com/O7c8Zp 在此处查看实时示例: http : //ideone.com/O7c8Zp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++中char和char *之间有什么区别? - What's the difference between char and char* in C++? C ++中&#39;char()&#39;和&#39;char&#39;有什么区别 - What is the difference between 'char()' and 'char' in c++ 新char [10]和新char(10)之间有什么区别 - What's the difference between new char[10] and new char(10) 在C ++中将整数参考值强制转换为(const char *)有什么影响?强制转换为char *和强制转换为const char *有什么区别? - What's the effect of casting integer reference value to (const char*) and what's the difference between cast to char* and cast to const char* in c++? 在C ++中存储字符串时,char *和char有什么区别? - What's the difference between char* and char when storing a string in C++? C / CPP中的char []和char [n]有什么区别? - What's the difference between char[] and char[n] in C/CPP? C++中char和signed char的区别? - Difference between char and signed char in c++? “char *”和“char * = new char[]”之间的C++区别 - C++ difference between “char *” and “char * = new char[]” c ++中两个&#39;char *&#39;铸造之间有什么区别 - What is the difference between two 'char*' castings in c++ c++ 中的字符串 a=“hello” 和字符串 a=(char *)“hello” 有什么区别? - what is the difference between string a=“hello” and string a=(char *)“hello” in c++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM