#include <stdio.h> int main(void) { char *p = "hello"; // 1. 定義一個指針變量 // 2. 稱指針(變量)p指向(字符串)常量"hello" // 3. 指針變量p的數值為字符串常量"hello"的地址 // 4. 一元運算符*是"間接運算符" printf ("*p: %c\n", *p); // 打印p指向地址的內容(為一個字符) printf ("p: %s\n", p); // 打印p指向的字符串(遇'\0'結束) printf ("p value: %p\n", p); // 打印p的數值(即字符串常量"hello"的地址) printf ("&p: %p\n", &p); // 打印指針變量p的地址 return 0; }
輸出內容:
*p: h(p指向地址的字符值)
p: hello(p指向地址開始的字符串,遇'\0'結束)
p value: 0x8048530(字符串常量"hello"的地址)
&p: 0xbfbe6eec(指針變量p的地址)