天天看點

關于C++中指針的一些了解

#include <bits/stdc++.h>

using namespace std;
int main() {
    char *p;
    p=(char *)malloc(10* sizeof(char));
    strcpy(p,"china");
    cout<<p<<endl;
    cout<<*(p+1)<<endl;
    //*p為"china"首位址  則系統會将p所指向的字元化為記憶體位址,并從那裡開始讀取……
    // (例如字元串開頭為'0'則從記憶體位址0x00000030開始讀取
    printf("%c\n",*p);
    //是指從指針p所指向的位元組開始輸出,直到讀到'\0'字元
    printf("%s\n",p);
    free(p);
    return 0;
}
/**
 * 指針 int *p=&a;
 * p存儲着a的記憶體位址,*p指向a中内容
 * char *p="china"
 * p存儲着字元串的記憶體首位址,*p指向字元串首位址即'c'
 */