#include <iostream>
int main(void)
{
char *sPtr;
sPtr = "What truth?";
std::cout << sPtr << std::endl;
sPtr = "There is no spoon.";
std::cout << sPtr << std::endl;
sPtr = "Then you'll see, that it is not the spoon that bends, it is only yourself.";
std::cout << sPtr << std::endl;
return 0;
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:pointerdemo5.cpp
功能:示範 C++ 程式
作者:張凱慶
時間:西元 2013 年 1 月 */編譯執行結果如下

利用指標的算術運算,指標可存取字串中每個字元,例如
#include <iostream>
int main(void)
{
char *sPtr = "hello";
char *s;
s = sPtr;
while (*s != '\0') {
std::cout << *s << std::endl;
s++;
}
return 0;
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:pointerdemo5.cpp
功能:示範 C++ 程式
作者:張凱慶
時間:西元 2013 年 1 月 */編譯執行結果如下

當然,字串也可以用字元陣列處理,喜歡用指標或陣列就看個人偏好囉!
| 中英文術語對照 | |
|---|---|
| 字串 | string |
| 指標 | pointer |
| 型態 | type |
| 變數 | variable |
您可以繼續參考
複合資料型態
相關目錄
回 C++ 快速導覽
回 C++ 教材
回首頁
參考資料
C++ reference
cplusplus.com
Cprogramming.com C++ Tutorial
C++ Primer, Fourth Edition, Stanley B. Lippman...
沒有留言:
張貼留言