c语言中没有字符串的类型,
所以对字符串操作,有两种形式:可以用字符指针,或者字符串数组(这里的指针变量c, 系统会为其重新分配内存)
(1)用字符数组存放一个字符串
char string[]="Linux C";
printf("%s\n",string);
string是一个字符数组名,它同时也是该字符数组的首地址。
(2)用字符串指针来指向字符串
如果不想定义字符数组,就可以只用一个指向字符串的指针,叫“字符串指针”,例如:
char *p="Linux C";
printf("%s\n",p);
“Linux C”是一个字符串常量。C语言对于字符串常量的处理方法:在内存中开辟一个字符数组来存储该字符串常量,并把开辟出的字符数组的首地址赋给p。
遍历字符串代码:
#include<stdio.h>
#include<string.h>
void main() {
char str[] = "hello world";
int len = strlen(str), i;
for (i = 0; i < len; i++) {
printf("%c", str[i]);
}
printf("\n");
char *p = str;
for (i = 0; i < len; i++) {
printf("%c", *(p++));
}
printf("\n");
for (i = 0; i < len; i++) {
printf("%c", *(str + i));
}
system("pause");
}
/*
运行结果:
hello world
hello world
hello world
*/
字符数组归根结底还是一个数组,上面中定义的字符串数组,str也可以认为是一个指针,指针以及数组方面操作均适用。
除了字符数组外,C语言还支持直接使用一个指针指向字符串的方式来表示字符串,代码如下:
char *str = "hello C language";
以此方式定义的字符串,同样可以对此字符串进行多样的操作。为什么可以用字符数组表示字符串,非要弄个指针来表示字符串呢?这其中定有深意。原来它们最根本的区别是在内存中的存储区域不一样,字符数组存储在全局数据区或栈区,而以指针形式表示的字符串却存储在常量区。全局数据区和栈区的字符串(也包括其他数据)有读取和写入的权限,而常量区的字符串(也包括其他数据)只有读取权限,没有写入权限。一句话概括:数组形字符串存放在全局数据区或栈区,可读可写。指针字符串存放在常量区,只读不能写。
验证了https://www.cnblogs.com/kelamoyujuzhen/p/10454129.html中的说法
参考自https://blog.csdn.net/u013812502/article/details/81196367
拷贝字符串方式
#include <stdio.h>
int main()
{
char a[] = "Linux C Program", b[20], c[20], d[20];
int i;
//(1)数组
for (i = 0; a[i] != '\0'; i++)
b[i] = a[i];
b[i] = '\0';
//(2)以数组首地址做基准的指针运算
for (i = 0; *(a + i) != '\0'; i++)
*(c + i) = *(a + i);
*(c + i) = '\0';
//(3)用字符串指针
char *p1, *p2;
p1 = a;
p2 = d;
for (; *p1 != '\0'; p1++, p2++)
*p2 = *p1;
*p2 = '\0';
printf("%s\n", a);
printf("%s\n", b);
printf("%s\n", c);
printf("%s\n", d);
return 0;
}
/*
运行结果:
Linux C Program
Linux C Program
Linux C Program
Linux C Program
*/
参考自https://blog.csdn.net/str999_cn/article/details/78596812
练习:
#include<iostream>
#include<string>
using namespace std;
//c语言中没有字符串的类型,
//所以对字符串操作,有两种形式:可以用字符指针,或者字符串数组(这里的指针变量c, 系统会为其重新分配内存。)
//c++编译器把char* 当成c语言字符串
//编译器把char*当成数组
//编译器直接把char* p[]当成字符串数组
void test01() {
char ch[] = "word";
char* p = ch;
std::cout << *p << std::endl;// 输出:w
std::cout << p << std::endl;// 输出:word
}
void test02() {
char ch = 's';
char* p = &ch;
std::cout << *p << std::endl;//输出s
std::cout << p << std::endl;//有时候会输出乱码,
//因为编译器把char*当成数组,会从字符开始输出直至遇到一个空字符
}
void test03() {
char c[10] = "hello";//字符数组,也即是C风格字符串
char ca[] = "hello world";
char ch[] = "word";
char* p = ch;//这里表明char* 其实是字符数组
char* pc[] = { c,ca,p };//正确赋值,数组名也是指针(指向数组首元素)
std::cout << p << std::endl;//word
std::cout << pc[0] << std::endl;//hello
std::cout << pc[1] << std::endl; //hello world
std::cout << pc[2] << std::endl;//word
}
void test04() {
string s = "string";
string *p = &s;
char * c = "hello";
cout << *c << endl; //h
cout << c << endl; //hello
cout << s << endl; //string
cout << *p << endl; //string
//*p 解引用输出的是整个字符串
//而 p 输出的是字符串的首地址
cout << p << endl; //000000565A8FFA78
}
int main() {
//test01();
//test02();
//test03();
test04();
return 0;
}
参考自https://blog.csdn.net/holyweng/article/details/82121436
char *s=NULL ;
char *ss= "123";
int len = strlen(ss);
s = (char*)malloc(len + 1); //strlen长度计算不包括'\0' 所以此处加一
strcpy(s, ss);
cout << s << endl; //123
strcpy的使用必须是在两个char * 之间