参考于:
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;
}