字元串變量名即是首位址,是以無需新定義字元串指針,隻定義字元串就可以了
#include <iostream>
using namespace std;
int main()
{
//首先明确:字元串的名稱既是字元串的首位址,是以str與str_ip完全一樣
char str[]="hello world!";
char* str_ip=str;//注意無&
//以下兩個輸出完全一樣
cout << str << endl;
cout << str_ip << endl;
//以下兩個輸出完全一樣
cout << (void*)str << endl;
cout << (void*)str_ip << endl;
//循環列印字元串,主要是用指針++
for(int i=0;i<sizeof(str)/sizeof(str[0]);i++)
{
cout << *(str+i) << endl;
}
cout << sizeof(str) << endl;
cout << sizeof(str[0]) << endl;
cout << sizeof(str)/sizeof(str[0]) << endl;
return 0;
}
hello world!
hello world!
0xffade2a4
0xffade2a4
he
l
l
o
w
o
r
l
d
!
13
1
13