数组与字符串小总结

C++中字符串为const char *类型

sizeof对变量名取值结果为内容大小,对字符串取值为字符串大小包括‘\0’

strlen函数声明为size_t strlen(char const* str),对变量名取值或者对字符串直接取值结果均为字符串长度但是不包括'\0'。


#include<iostream>
using namespace std;
int main() {
	 //char* strs = "welcome";//"welcome"的类型为const char *
	 const char* strs = "welcome";
	 cout << typeid(strs).name() << endl;//char const *
	cout << sizeof(strs) << endl;//4
	cout << strlen(strs) <<endl;//7
	cout << strlen("welcome") << endl;//7
	cout << sizeof("welcome") << endl;//8
	 const char* str[] = { "welcome","to","CascoSigLtd","Shanghai" ,"jjjj"};
	 cout << size(str) << endl;//元素数量5
	cout << sizeof(str) << endl;//20
	cout << sizeof(str) / size(str) << endl;//4
	cout << typeid(str).name() << endl;//char const * [4]
	//cout << strlen(str) << endl;//str为char**,而size_t strlen (char const* str)
	const char** p = str + 1;
	cout << typeid(p).name() << endl;//char const **
	str[0] = (*++p) + 2;//(++*p)、(*p++),注:p为指向指针的指针
	cout << typeid(str[0]).name() << endl;//char const *
	str[1] = *(p + 1);
	str[2] = p[1] + 5;
	str[3] = p[0] + (str[2] - str[1]) + 2;
	printf("第一个:%s\n", str[0]);//空
	printf("第二个:%s\n", str[1]);//Shanghai
	printf("第三个:%s\n", str[2]);//hai
	printf("第四个:%s\n", str[3]);//第一个:%s
	return 0;
}
#include<iostream>
using namespace std;
//int gbits(int n) {
//	n = n - 1;
//	n |= n >> 1;
//	n |= n >> 2;
//	n |= n >> 4;
//	n |= n >> 8;
//	n |= n >> 16;
//	n = n + 1;
//	return n;
//}
int f(int p) {
	static int x;
	x = x + p;
	return x;
}
int f(int &p) {
	static int x;
	x = x + p;
	return x;
}
int main() {
	/*int**pp, * p, a = 10, b = 20;
	pp = &p;
	p = &a;
	p = &b;
	printf("%d,%d\n", *p, **pp);*/
	/*char w = 'a';
	int x = 2;
	float y = 3;
	double z = 2.5;
	auto ans = w * x + z - y;
	cout << w << endl;
	cout << (int)w << endl;
	cout << x << endl;
	cout << y << endl;
	cout << z << endl;
	cout << ans << endl;
	cout << typeid(ans).name() << endl;*/
	/*int a[] = { 1,2,3 }, b[] = { 4,2,5 };
	int* p, * q;
	int  i, x;
	p = a;
	q = b;
	cout << *(p + 2) << " " << *(q + 2)<<" " << *p + 5 << endl;
	for (int i = 0; i < 3; i++) {
		if (*(p + i) == *(q + i))x = *(p + i) * 2;
	}
	cout << x << endl;*/
	int n = 4;
	/*int first = n >> 1;
	int second = n >> 2;
	int third = n << 1;
	int four = n | n + 1;
	
	cout <<"n:" << n << endl;
	cout <<"first:"<< first << endl;
	cout << "second:" << second << endl;
	cout <<"third:" << third << endl;
	cout << "four:" << four << endl;
	cout << gbits(n) << endl;*/
	cout << "f():" << f(1) << endl;
	cout<< f(1) << endl;
	//cout << "f(&):" << f(2) << endl;
	return 0;
}

*(p+i)=a[i]

*p+i=a[i]+i;

注:第一次以此形式记录,比较随心,还望海涵,后面再接再厉,勿喷,谢谢。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凤毛棱角

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值