char和string数组

char string数组

在这里插入图片描述

sizeof strlen

#include <stdio.h>
#include <vector>
#include <string>
#include<iostream>
#include <algorithm>

using namespace std;

int main()
{
	char* a = new char[30];
	cout << "未初始化char数组--sizeof = " << sizeof(a) << endl;
	cout << "未初始化char数组--strlen = " << strlen(a) << endl;
	cout << "**************************" << endl;
	char* b = "hello";
	cout << "hello 数组--sizeof = " << sizeof(b) << endl;
	cout << "hello 数组--strlen = " << strlen(b) << endl;
	cout << "**************************" << endl;
	char* c;
	cout << "char数组指针--sizeof = " << sizeof(c) << endl;
	//cout<<"char数组指针--strlen = "<<strlen(c)<< endl;  编译通过,运行出错
	cout << "**************************" << endl;
	char* d = new char[30];
	d = "hello";
	cout << "为已开辟空间的数组赋值--sizeof = " << sizeof(d) << endl;
	cout << "为已开辟空间的数组赋值--strlen = " << strlen(d) << endl;
	cout << "**************************" << endl;
	char* e = new char[30];
	e[0] = 'h'; e[1] = 'e'; e[2] = 'l'; e[3] = 'l'; e[4] = 'o'; e[5] = '\0';
	cout << "hello 数组 分配的内存未填满,且加\0--sizeof = " << sizeof(e) << endl;
	cout << "hello 数组 分配的内存未填满,且加\0--strlen = " << strlen(e) << endl;
	cout << "**************************" << endl;
	char* ee = new char[30];
	ee[0] = 'h'; ee[1] = 'e'; ee[2] = 'l'; ee[3] = 'l'; ee[4] = 'o'; ee[5] = '\0';
	ee[6] = '\0';
	cout << "两个\\0的char数组--sizeof = " << sizeof(ee) << endl;
	cout << "两个\\0的char数组--strlen = " << strlen(ee) << endl;
	cout << "**************************" << endl;
	char* eee = new char[30];
	eee[0] = 'h'; eee[1] = 'e'; eee[2] = 'l'; eee[3] = 'l'; eee[4] = 'o';
	cout << "未手动添加\\0的char数组--sizeof = " << sizeof(eee) << endl;
	cout << "未手动添加\\0的char数组--strlen = " << strlen(eee) << endl;
	cout << "**************************" << endl;
	char* eeee = new char[30];
	for (int i = 0; i<30; i++)
	{
		eeee[i] = 'h';
	}
	cout << "数组分配内存填满,未加\\0--sizeof = " << sizeof(eeee) << endl;
	cout << "数组分配内存填满,未加\\0--strlen = " << strlen(eeee) << endl;
	cout << "**************************" << endl;
	char* eeeee = new char[30];
	for (int i = 0; i<29; i++)
	{
		eeeee[i] = 'h';
	}
	cout << "数组分配内存填满,加\\0--sizeof = " << sizeof(eeeee) << endl;
	cout << "数组分配内存填满,加\\0--strlen = " << strlen(eeeee) << endl;
	cout << "**************************" << endl;
	string s("hello");
	cout << "hello string--sizeof = " << sizeof(s) << endl;
	cout << "hello string--strlen = " << strlen(s.c_str()) << endl;
	system("pause");
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值