sizeof()和strlen()的检测与对比

//*****sizeof函数的用法******


#include <iostream>
using namespace std;


void printValues(const int ia[], size_t size)
{
for (size_t i = 0; i != size; ++i)
{
cout << ia[i] << endl;
}
}


int main()
{
int j[] = {0,1};
printValues(j, sizeof(j) / sizeof(*j));


cout <<sizeof(j) <<endl;
cout <<sizeof(*j) <<endl;
cout <<endl;


cout <<"sizeof函数的检测:"<<endl;
char *str1 = "absde";
char str2[] = "absde";
char str3[8] = {'a'};
char str4[] = "0123456789";
cout <<"前面是sizeof(str1)"<<"\t"<<"后面的strlen(str1)函数作为对比的"<<endl;
cout<<"*str1:"<<sizeof(str1)<<"\t\t\t"<<strlen(str1)<<endl;
cout<<"str2:"<<sizeof(str2)<<"\t\t\t\t"<<strlen(str2)<<endl;
cout<<"str3:"<<sizeof(str3)<<"\t\t\t\t"<<strlen(str3)<<endl;
cout<<"str4:"<<sizeof(str4)<<"\t\t\t"<<strlen(str4)<<endl;


cout <<"同时检测到一个字符型占1个字节"<<endl;
cout <<endl;


int a = 123;
int *a1 = &a;
int a2[] = {1,2,3,4,5,6};
int a3[8] = {3};
int a4[] = {0,1,2,3,4,5,6,7,8,9};
cout <<"sizeof(a1): "<<sizeof(a1)<<endl;
cout <<"sizeof(a2): "<<sizeof(a2)<<endl;
cout <<"sizeof(a3): "<<sizeof(a3)<<endl;
cout <<"sizeof(a4): "<<sizeof(a4)<<endl;
cout <<"同时检测到一个整型占4个字节";
cout <<endl;
cout <<endl;


char const keyword[][10] = {"do","for","if","register","return","switch","while"};
cout<< sizeof(keyword)<<endl;//整个数组的长度
cout<< sizeof(keyword[0])<<endl;//每个元素所占的字节
cout<< sizeof(keyword) / sizeof(keyword[0])<<endl;//数组元素的个数


system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值