Zi 字媒體
2017-07-25T20:27:27+00:00
C++ STL 容器基本範例實驗測試 [00_CPP_STL_String ->純C 字串範例]
資料來源:https://www.cnblogs.com/skyfsm/p/6934246.html
http://larry850806.github.io/2016/06/06/STL1/
http://larry850806.github.io/2016/06/06/STL2/
GITHUB 完整資料: https://github.com/jash-git/Base_CPP_STL_Example
#include
#include
#include /* malloc, free, rand */
using namespace std;
void Pause()
{
printf("Press Enter key to continue...");
fgetc(stdin);
}
int main()
{
//---
//C字串範例
char* s1 = "Hello SYSU!"; //创建指针指向字符串常量,这段字符串我们是不能修改的
printf("s1=%s\n",s1);
//想要创建 可以修改的字符串,我们可以使用数组分配空间
char s2[20] = "Hello SYSU!";
printf("s2=%s\n",s2);
//或者这样
char s3[] = "Hello SYSU!";
printf("s3=%s\n",s3);
//当然我们也可以动态分配内存
char* s4 = (char*)malloc(20);
gets(s4);//接收輸入字串
printf("s4=%s\n",s4);
free(s4);
//---C字串範例
Pause();
return 0;
}
用到的相關技巧:
C/C++自製pause函數
純C動態配置記憶體
純C字串宣告[可修改/不可修改]
寫了
5860316篇文章,獲得
23313次喜歡