#include <stdio.h> int main() { char str[]="China Beijing Fujian"; //定義數組str並進行初始化 char *pstr; //定義指針變量pstr pstr=&str[6]; //初始化pstr,並使指針變量pstr指向數組str printf("str:%s,str"); printf("pstr指向str[6]:%c\n,*pstr"); return 0; }
結果:數組
str:China Beijing Fujian pstr指向str[6]:B
#include <stdio.h> int main(void) { char str[]="China Beijing Fujian"; //定義數組str並進行初始化 char *pstr; //定義指針變量pstr printf("str:%s\n",str); //輸出數組str,%s表示字符串 pstr=str; //初始化指針變量pstr,pstr指向數組str;pstr和str指向數組str首地址 printf("pstr->str:%s\n",pstr); //以%s的形式輸出pstr指向的數組元素 printf("&str=%p\n",&str); //%p指針的值,輸出數組名str的地址,即&str。連續數組空間的首地址,即str[0]的地址 printf("pstr=%p\n",pstr); //輸出指針變量pstr的值。由於pstr指向數組str,pstr的值就是str的地址,也是數組元素,str[0]的地址 printf("&str[0]=%p\n",&str[0]); return 0; }
結果:ui
str:China Beijing Fujian pstr->str:China Beijing Fujian &str=0x7fffd12e1c80 pstr=0x7fffd12e1c80
2-3spa
#include <stdio.h> int main(void) { char str[]="China Fujian"; char *pstr; pstr=str; while(*pstr) { printf("%c:%p\n",*pstr,pstr); //%c單個字符,第一次執行時,pstr指向str[0],輸出str[0]以及&str[0] pstr++; //讓指針指向下一個數組元素 } return 0; }
結果:指針
C:0x7fff13c1f1b0 h:0x7fff13c1f1b1 i:0x7fff13c1f1b2 n:0x7fff13c1f1b3 a:0x7fff13c1f1b4 :0x7fff13c1f1b5 F:0x7fff13c1f1b6 u:0x7fff13c1f1b7 j:0x7fff13c1f1b8 i:0x7fff13c1f1b9 a:0x7fff13c1f1ba n:0x7fff13c1f1bb
#include <stdio.h> #define WEEKNUM 7 //定義一個宏 int main(void) { int temp; int week[WEEKNUM]={1,2,3,4,5,6,7}; printf("please input today is:"); scanf("%d",&temp); if(temp<=WEEKNUM) { printf("tomorrow is :%d\n",week[temp]); //%d十進制有符號字符 } else { printf("Error \n"); } return 0; }
結果:blog
please input today is:4 tomorrow i
2-5字符串
#include <stdio.h> #define LEN 8 int main(void) { char i; char str[LEN]="Fujian"; printf("str:%s\n",str); for(i=0;i<LEN;i++) { printf("%c:%p\n",str[i],&str[i]); } return 0; }
結果:input
str:Fujian F:0x7fff3f5c5d20 u:0x7fff3f5c5d21 j:0x7fff3f5c5d22 i:0x7fff3f5c5d23 a:0x7fff3f5c5d24 n:0x7fff3f5c5d25 :0x7fff3f5c5d26 :0x7fff3f5c5d27
#include <stdio.h> #define LEN 10 int main(void) { char str[LEN]={'A','B','C','D','E','F','G','H','I','J'}; char idx,*pstr; pstr=str; printf("please input (0-9)and ENTER:\n"); scanf("%d",&idx); if(idx<LEN) { printf("The character is:%c\n",*(pstr+idx)); //patr指向數組str,即指向數組的第一個元素str[0],當運行pstr+idx運算後,,指針pstr指向str[idx],*(patr+idx)就是數組元素str[idx] } else { printf("The idx is overflow \n"); } return 0;
結果:it
please input (0-9)and ENTER: 4 The character is:E
#include <stdio.h> #define LEN 15 int main(void) { char str[LEN]="Fujian.2018"; char idx,*pstr; for(idx=0;idx<LEN;idx++) { printf("%c",str[idx]); //下標法 } printf("\n"); pstr=str; while(*pstr) { printf("%c",*(pstr+idx)); //指針法 pstr++; } printf("\n"); return 0; }
結果:io
Fujian.2018
#include <stdio.h> int main(void) { char *str[]={"Fujian","Huian"}; //定義數組str並初始化 printf("str[0]:%s\n",str[0]); //%s一次性輸出一個字符串,若要一次性輸出一個字符串須要知道字符串的首地址,str[0]即爲首地址 printf("str[1]:%s\n",str[1]); return 0; }
結果:class
str[0]:Fujian str[1]:Huian
2-10
#include<stdio.h> int main() { char temp; char *str[]={"IllgalDay","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"}; printf("please input number:\n"); scanf("%d",&temp); if(temp<7) { switch(temp) { case 0: printf("Ingall day\n"); break; case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break; } } return 0; }
結果:
please input number: 1 Monday
#include <stdio.h> int main(void) { char *str="Fujian 2018"; //至關於 char *str; str ="Fujian 2018"; printf("%s\n",str); //指針變量str指向字符串的「Fujian 2018」的首地址,這裏僅僅是指向不是賦值 return 0; }
結果:
Fujian 2018
#include <stdio.h> int main(void) { char str[]="Fujian 2018"; //定義數組,並進行初始化 printf("%s\n",str); return 0; }
結果:
Fujian 2018
利用數組形式定義字符串就須要知道字符串大小,而指針形式則沒有這個限制
2-13
#include <stdio.h> int main() { char *str="Fujian 2018"; printf("%s\n",str); //%s字符串 printf("%p\n",str); //%p指針,輸出str,字符串首地址 while(*str) { printf("%c",*str); //輸出字符 printf(" %p\n",str); //輸出字符地址 str++; } return 0; }
結果:
Fujian 2018 0x4006c8 F 0x4006c8 u 0x4006c9 j 0x4006ca i 0x4006cb a 0x4006cc n 0x4006cd 0x4006ce 2 0x4006cf 0 0x4006d0 1 0x4006d1 8 0x4006d2
一步一個腳印......