來博客園今天恰好兩年了,兩年前開始學編程。算法
忙碌近兩個月,項目昨天上線了,真心不容易,也不敢懈怠,接下來的問題會更多。這兩天調試服務器,遇到很多麻煩。編程
剛出去溜達了一下,晚上天涼了,如今手感受涼的有點不靈活了都。大夥多注意身體!數組
繼續個人C。發現個問題,本身的文章排版很醜,之後也要多注意。服務器
printf("hello world");函數
printf接受的是一個指向字符數組第一個字符的指針。也就是說,字符串常量可經過一個指向其第一個元素的指針訪問。測試
char *p;ui
p = "hello world"; //將一個指向字符串數組的指針賦值給p。該過程沒有進行字符串的複製,只是涉及到指針的操做。C語言沒有提供將整個字符串做爲一個總體進行處理的運算符。spa
char s[] = "hello world"; //定義一個字符數組指針
char *p = "hello world"; //定義一個指針調試
兩種聲明的區別:
s是一個僅足以存放初始化字符串及空字符'\0'的一維數組,數組中的單個字符能夠修改。
p始終指向同一個存儲位置,其初始值指向一個字符串常量,以後它能夠被修改以指向其餘地址,若是試圖修改字符串的內容,結果是沒有定義的。
//複製字符串
1 #include <stdio.h> 2 void strcpy1(char *s, char *t); 3 4 main(){ 5 char t[] = "hello world"; 6 char s[] = ""; 7 strcpy1(s,t); 8 printf("%s\n",s); //hello world 9 } 10 /******將指針t指向的字符串複製到指針s指向的位置,使用數組下標實現***/ 11 void strcpy1(char *s, char *t){ 12 int i = 0; 13 while((s[i] = t[i]) != '\0'){ 14 i++; 15 } 16 }
1 #include <stdio.h> 2 void strcpy2(char *s, char *t); 3 4 main(){ 5 char t[] = "hello world"; 6 char s[] = ""; 7 strcpy2(s,t); 8 printf("%s\n",s); //hello world 9 } 10 /******將指針t指向的字符串複製到指針s指向的位置,使用指針實現***/ 11 void strcpy2(char *s, char *t){ 12 while((*s = *t) != '\0'){ 13 s++; 14 t++; 15 } 16 /** 17 //簡寫 18 while((*s++=*t++) != '\0') 19 ; 20 **/ 21 /** 22 //再簡寫 23 while(*s++=*t++) 24 ; 25 **/ 26 }
剛遇到這個警告:conflicting types for built-in function 'strcpy'
函數命名衝突了
//比較兩字符串
1 #include <stdio.h> 2 int strcmp(char *s, char *t); 3 4 main(){ 5 char t[] = "hello world"; 6 char s[] = "helloabc"; 7 printf("%d\n",strcmp(s,t)); //65 8 } 9 /****比較兩字符串順序***/ 10 int strcmp(char *s, char *t){ 11 int i; 12 for(i=0; s[i]==t[i]; i++){ 13 if(s[i] == '\0'){ 14 return 0; 15 } 16 } 17 return s[i] - t[i]; 18 }
#include <stdio.h> int strcmp(char *s, char *t); main(){ char t[] = "hello world"; char s[] = "helloabc"; printf("%d\n",strcmp(s,t)); //65 } /****比較兩字符串順序***/ int strcmp(char *s, char *t){ for(; *s==*t; s++,t++) { if(*s == '\0'){ return 0; } } return *s - *t; }
一個函數實現或一種算法的實現,仍是須要用數據去模擬,而後找出規律。就上例,做簡單分析:
s1 "hello world";
s2 "helloabc";
for循環,i=0,s[0]=t[0],依此類推,s[4]=t[4],當i=5時,s[5]是一個空格,t[5]=a,s[5]!=t[5],跳出for循環,返回a字符與空格字符的差,97-32=65。假如t[5]也是一個空格的話,繼續下一個比較,若是s[6]==‘\0’的話,說明s[5]仍是等於t[5],返回0。
之後儘可能都要去多分析原理,加深記憶。
指針數組及指向指針的指針
指針自己也是變量,因此它也能夠其餘變量同樣存儲在數組中。
二維數組
今天是2013年的第300天,今年只剩65天,你們多多珍惜吧!很巧的是,以前的測試中字符a-空格恰好也是65。
1 #include <stdio.h> 2 int day_of_year(int year, int month, int day); 3 void month_day(int year, int yearday, int *pmonth, int *pday); 4 5 static char daytab[2][13] = { 6 {0,31,28,31,30,31,30,31,31,30,31,30,31}, 7 {0,31,29,31,30,31,30,31,31,30,31,30,31} 8 }; 9 main(){ 10 printf("%d\n", day_of_year(2013, 10, 27)); //300 11 int pmonth = 0; 12 int pday = 0; 13 int year = 2013; 14 int yearday = 300; 15 month_day(year, yearday, &pmonth, &pday); 16 printf("%d年第%d天是%d月%d日\n",year, yearday,pmonth,pday); //2013年第300天是10月27日 17 return 0; 18 } 19 20 int day_of_year(int year, int month, int day){ 21 int i, leap; 22 leap = (year%4 == 0 && year%100 != 0) || (year %400 == 0); 23 for(i=1; i<month; i++){ 24 day += daytab[leap][i]; 25 } 26 return day; 27 } 28 29 void month_day(int year, int yearday, int *pmonth, int *pday){ 30 int i, leap; 31 leap = (year%4 == 0 && year%100 != 0) || (year %400 == 0); 32 for(i=1; yearday>daytab[leap][i]; i++){ 33 yearday -= daytab[leap][i]; 34 } 35 *pmonth = i; 36 *pday = yearday; 37 }
附:
一我的晚上出去打了10斤酒,回家的路上碰到了一個朋友,恰巧這個朋友也是去打酒的。不過,酒家已經沒有多餘的酒了,且此時天色已晚,別的酒家也都已經打烊了,朋友看起來十分着急。因而,這我的便決定將本身的酒分給他一半,但是朋友手中只有一個7斤和3斤的酒桶,兩人又都沒有帶稱,如何才能將酒平均分開呢?
一天,小趙的店裏來了一位顧客,挑了20元的貨,顧客拿出50元,小趙沒零錢找不開,就到隔壁小韓的店裏把這50元換成零錢,回來給顧客找了30元零錢。過一會,小韓來找小趙,說剛纔的是假錢,小趙立刻給小李換了張真錢。問:在這一過程當中小趙賠了多少錢?
原文做者:lltong,博客園地址:http://www.cnblogs.com/lltong/