字符串I'm a student存储方式

来源:百度知道 编辑:UC知道 时间:2024/06/28 16:41:33
假设有一个字符串“I'm a student”,给出其存储方式

在C中可以用数组和在静态储存区保存两种形式储存。不过要注意的是在静态储存区储存的就只是只读的了。
在C++中的话多了一个方式,就是以字符串对象的形式储存。下面的代码将这三种方式都演示一遍。以C++的IO控制输出结果。

#include <iostream.h>
#include <string>
void main()
{
char s1[] = "I'm a student";
char *ps = "I'm a student";
string s2 = "I'm a student";
cout << "数组的形式: " << s1 << endl;
cout << "静态储存区的形式:" << ps << endl;
cout << "string对象的形式:" << s2 << endl;
}
输出为:
数组的形式: I'm a student
静态储存区的形式:I'm a student
string对象的形式:I'm a student

在C系统中字符串是用字符数组来存放的

比如你定义一个字符串
string a=“I'm a student”
那就相当于你定义了一个字符数组
char a[13]=“I'm a student”

所以可以通过数组元素的访问方式来访问字符串。这就是答案

char a[14] = “I'm a student”;
这样就可以了。

字符串是用字符数组来进行存储的,字符串和字符数组的区别就在于字符串存储在字符数组里最后要加一个结束符'