字符指针 与 字符数组

参考于:
https://www.cnblogs.com/xiximayou/p/12121625.html
https://www.cnblogs.com/wzjhoutai/p/6940920.html

以一维为例
字符指针 vs 字符数组

1 赋值

1.0 定义初始化

字符数组字符指针
char str1[] = "asdas";char str2[100] = "asdas";char *str3 = "asdas";
sizeof(str1) = 6
(5个字符 + ‘\0’)
sizeof(str2) = 100
(开辟空间容量)
sizeof(str3) = 8
(char* 的长度, sizeof(char*) = 8)
sizeof(str1[0]) = sizeof(str1[0]) = sizeof(str1[0]) = 1
sizeof(char) = sizeof(‘a’) = 1
(字符‘a’)

字符指针指向 字符串,字符数组是数组
字符串长度 = 字符个数
字符数组长度 = 字符个数 + 1

测试代码:

#include<bits/stdc++.h> 
#define num 100
using namespace std;

int main()
{
	char str1[] = "asdas";
	cout << "str1[] = " << str1 <<endl;
	cout << "sizeof(str1) = " << sizeof(str1) << endl;
	cout << "sizeof(str1[0]) = " << sizeof(str1[0]) << endl <<endl;
	
	char str2[100] = "asdas";
	cout << "str2[100] = " << str2 <<endl;
	cout << "sizeof(str2) = " << sizeof(str2) << endl;
	cout << "sizeof(str2[0]) = " << sizeof(str2[0]) << endl <<endl;
	
	char *str3 = "asdas";
	cout << "*str3 = " << str3 <<endl;
	cout << "sizeof(str3) = " << sizeof(str3) << endl;
	cout << "sizeof(str3[0]) = " << sizeof(str3[0]) << endl;
	cout << "sizeof(char) = " << sizeof(char) << endl;
	cout << "sizeof('a') = " << sizeof('a') << endl ;
	cout << "sizeof(char*) = " << sizeof(char*) << endl <<endl;
	
	system("pause");
	return 0;
}

1.1 直接初始化赋值

字符数组 和 字符指针 可以直接初始化赋值!

char str1[20] = "ni hao";
char *str2 = "ni hao";

1.2 非初始化赋值

字符数组(str)字符指针(str)
strcat(a, b);可以不可以
strcpy(a, b);可以不可以
str[i] = ch;可以不可以
str++;不可以可以
str = string;不可以可以

字符数组本身的值 可以一个一个的用 等号赋值的办法 改变:

	char str1[num] = "qwe";
	int i;
	for(i = 0; i < num; i++)
		str1[i] = 'd';

字符指针 指向 的值不可以改变!str[3] = ‘s’;会报错的
指针本身 可以用等号赋值的办法 改变:str = string; 即可

字符串"abc"是不是常量?

赋给字符数组时,不是常量(是char *,不是const char *),因为此时数组内容可改变
赋给字符指针时,是常量(是const char *,不是char *),此时字符指向的 “abc” 不可改变

strcat(a, b) 和 strcpy(a, b):

 char *strcat(char *dest, const char *src)
 char *strcpy(char* dest, const char *src);

所以
a 只能是 容量足够 的字符数组!
b 是字符数组 或 字符指针 均可!

代码测试:

#include<bits/stdc++.h> 
#define num 100
using namespace std;

int main()
{
	
	//1 初始化赋值 
//	char str1[num] = "ni hao";
//	char *str2 = "ni hao";
	
	//2 非初始化赋值 
//	char str1[num];
//	str1 = "ni hao"; // 错误 
//	char *str2;
//	str2 = "ni hao";
	
	//2.1 字符数组操作 
	cout << "字符数组操作" << endl << endl; 
	char str1[num] = "qwe";
	char *str2 = "sdf";
	int i;
	cout << "str1[num] = " << str1 << endl;
	cout << "*str2 = " << str2 << endl << endl;
	//strcat
	strcat(str1, "23"); 
	cout << "strcat(str1,\"23\")后:" << endl;
	cout << "str1 = " << str1 << endl << endl;
	//strcpy 
	strcpy(str1, str2);
	cout << "strcat(str1,str2)后:" << endl;
	cout << "str1 = " << str1 << endl << endl;
	//for赋值  str1[i] = 'd';
	for(i = 0; i < 4; i++)
		str1[i] = 'd';
	cout << "str1[num] for循环逐个赋值'd'后:" << endl;
	cout << "str1[num] = " << str1 << endl << endl << endl;
	// str1++ ; str1 = str2
//	str1++; // 错误 
//	str1 = str2; // 错误 
	
	
	// 字符指针操作
	cout << "字符指针操作" << endl << endl;	 
	str2 = "asdasdasd";
	char *str3 = "1244";
	cout << "*str2 = " << str2 << endl;
	cout << "*str3 = " << str3 << endl << endl;
	//strcat
//	strcat(str2, "qw"); // 错误
	//strcpy
//	strcpy(str2, str3); // 错误
	// str2[0] = '9'
//	str2[0] = '9'; // 错误
	
	//str2++ 
	str2++; 
	cout << "str2++后:" << endl;
	cout << "*str2 = " << str2 << endl << endl;
	// = 
	str2 = "zxcasd"; 
	cout << "str2 = \"zxcasd\"后:" << endl;
	cout << "*str2 = " << str2 << endl << endl;
	
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

qq_1403034144

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

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

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

打赏作者

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

抵扣说明:

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

余额充值