C ++:一个使用字符数组的sizeof大小 [英] C++: size of a char array using sizeof

查看:153
本文介绍了C ++:一个使用字符数组的sizeof大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看在C ++下面这段code的:

Look at the following piece of code in C++:

char a1[] = {'a','b','c'};
char a2[] = "abc";
cout << sizeof(a1) << endl << sizeof(a2) << endl;

虽然的sizeof(char)的为1个字节,为什么输出节目的sizeof(A2)为4,不是3(在的情况下, A1 )?

Though sizeof(char) is 1 byte, why does the output show sizeof(a2) as 4 and not 3 (as in case of a1)?

推荐答案

C字符串包含null终止符,因此增加一个字符。

C-strings contain a null terminator, thus adding a character.

这实质上:

char a2[] = {'a','b','c','\0'};

这篇关于C ++:一个使用字符数组的sizeof大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆