#include <iostream>
#include <string>
using namespace std;
//sizeof是运算符,计算在栈中声明变量或变量类型所占内存空间的字节数,不是实际长度
//strlen是函数,遇到第一个'\0'停止计数,返回实际字符串或字符数组的长度(不包括'\0')
int main()
{
//一、整型数组的大小
cout << "输出整型数组的大小:" << endl;
int a1[] = { 1, 2, 3, 4, 5 };
cout << sizeof(a1) << endl; //输出20(4*5)
cout << sizeof(a1) / sizeof(int) << endl; //输出5
int a2[10] = { 1, 2, 3, 4, 5 };
cout << sizeof(a2) << endl; //输出40(4*10)
cout << sizeof(a2) / sizeof(int) << endl; //输出10
cout << endl;
//二、字符串数组的大小
cout << "输出字符串数组的大小:" << endl;
char b1[] = { 'a', 'b', 'c', 'd', 'e', 'd' };
cout << sizeof(b1) << endl; //输出6(包含6个字符)
cout << strlen(b1) << endl; //因为字符串中没有'\0',所以输出随机值
char b2[] = { 'a', 'b', '\0' };
cout << sizeof(b2) << endl; //字符串中给定
#include <string>
using namespace std;
//sizeof是运算符,计算在栈中声明变量或变量类型所占内存空间的字节数,不是实际长度
//strlen是函数,遇到第一个'\0'停止计数,返回实际字符串或字符数组的长度(不包括'\0')
int main()
{
//一、整型数组的大小
cout << "输出整型数组的大小:" << endl;
int a1[] = { 1, 2, 3, 4, 5 };
cout << sizeof(a1) << endl; //输出20(4*5)
cout << sizeof(a1) / sizeof(int) << endl; //输出5
int a2[10] = { 1, 2, 3, 4, 5 };
cout << sizeof(a2) << endl; //输出40(4*10)
cout << sizeof(a2) / sizeof(int) << endl; //输出10
cout << endl;
//二、字符串数组的大小
cout << "输出字符串数组的大小:" << endl;
char b1[] = { 'a', 'b', 'c', 'd', 'e', 'd' };
cout << sizeof(b1) << endl; //输出6(包含6个字符)
cout << strlen(b1) << endl; //因为字符串中没有'\0',所以输出随机值
char b2[] = { 'a', 'b', '\0' };
cout << sizeof(b2) << endl; //字符串中给定