main(){ char *ps="this is a book"; int n=10; ps=ps+n; printf("%s/n",ps); } 運行結果為:
book 在程序中對ps初始化時,即把字符串首地址賦予ps,當ps= ps+10之后,ps指向字符“b”,因此輸出為"book"。
main(){ char st[20],*ps; int i; printf("input a string:/n"); ps=st; scanf("%s",ps); for(i=0;ps[i]!='/0';i++) if(ps[i]=='k'){ printf("there is a 'k' in the string/n"); break; } if(ps[i]=='/0') printf("There is no 'k' in the string/n"); } 本例是在輸入的字符串中查找有無‘k’字符。 下面這個例子是將指針變量指向一個格式字符串,用在printf函數中,用于輸出二維數組的各種地址表示的值。但在printf語句中用指針變量PF代替了格式串。 這也是程序中常用的方法。