字串
char s[] = "Hello World";
char *s = "Hello World";
char s[] 是個char array,含12個byte(包含結尾\0)
char *s 是一個 pointer 指向 char,s 指向 "Hello World" 這個string literal的起始記憶體位置
範例:
#include <iostream>
using namespace std;
int main() {
char s1[] = "Hello World";
char *s2 = "Hello World";
cout << "size of s1: " << sizeof(s1) << endl;
cout << "size of s2: " << sizeof(s2) << endl;
}
結果為:
size of s1: 12
size of s2: 4
結論
char *s速度較快
Last updated
Was this helpful?