The C Programming Language

  • 在binary search中得滤,如果全部都是通過ptr來操作原數(shù)組候址,需要注意mid = low + (high - low) / 2.
    • 原因在于指針是不可以相加的赏僧。而substraction is legal匿情,結(jié)果就是兩個指針(往往是同一數(shù)組內(nèi)的)的offset的差值。
  • 對于一個大小為n的數(shù)組s薪缆,訪問它的&s[n](i.e. s+n) is legal,dereferencing it is illegal.然而對于s[-1]是完全illegal秧廉。
  • 不要認(rèn)為一個struct的大小就是所有member大小之和。
    • 為了對齊的要求(內(nèi)存中)拣帽,there may be unnamed holes in a structure.
struct {
    char c;
    int i;
};

需要8個bytes,而不是5個疼电。sizeof works properly here.

for (ptr = head; ptr != NULL; p=p->Next) {
}

是walk through linked list idiom.

  • (the ptr type)malloc(sizeof(whatever you want)).malloc的返回值是void,需要顯式cast到想要的類型

  • 在C語言中减拭,用-表示空格蔽豺。printf中,%s的幾種變體:%.*s(接參數(shù)), %.10s(經(jīng)測試拧粪,只有precision小于字符串長度的時候才有效,沒有'.'也無效), %-10s(改為左對齊(默認(rèn)情況下是右對齊),字符不夠用空格補), %15.10s(取10個字符修陡,總共占15個字符長度,不夠的用空格補足可霎,right alignment), %-15.10s同上魄鸦,left alignment.

  • sprintf作用是把一個字符串格式化然后放入一個buffer字符串中,之后就可以使用這個字符串癣朗,而不必自己進(jìn)行對字符數(shù)組的操作拾因,十分方便.(buffer must be big enough to receive the result)

  • 實現(xiàn)minprintf的tricky bit is how minprintf walks along the argument list when list doesn't even have a name.

  • scanf: except the 1st, the other arguments, each of which must be a pointer, indicate where the corresponding converted input should be stored.

  • scanf: returning 0 means the next input character does not match the first specification in the format string.The next call to scanf resumes searching immediately after the last character already converted.

  • sscanf: reads from a string instead of the standard input.return the items assigned value successfully.

  • scanf: an input field is defined as a string of non-while space characters; it extends either to the next while space or until the field width.(WHITE-SPACE characters are blank, tab, newline(\n), carriage return(return key), veritcal tab, formfeed(進(jìn)紙頁))

  • No & is used with array name since it is a pointer.

  • scanf skips over white space(blank, tabs, newlines, etc) as it looks for input values.

  • scanf: to read input whose format is not fixed, it is often best to read a line at a time, then pick it apart with sscanf.(nice stratage!)

Example:

while (getline(line, sizeof(line)) > 0) {
    if (sscanf(line, "%d %s %d", &day, monthname, &year) == 3) {
        printf("valid: %s\n", line);
    }else if (sscanf(line, "%d/%d/%d", &month, monthname, &year) == 3) {
        printf("valid: %s\n", line);
    }else {
        printf("invalid %s\n", line);
    }
}
  • binsearch on K & R C:
int binsearch(int x, int v[], int n) {
    int low, high, mid;
    low = 0;
    high = n - 1;
    while (low <= high){
        mid = (low + high)/2;
        if (x < v[mid]) {
            high = mid - 1;
        }else if (x > v[mid]) {
            low = mid + 1;
        }else {
            return mid;//Found match
        }
    }
    return -1;//no match
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市旷余,隨后出現(xiàn)的幾起案子绢记,更是在濱河造成了極大的恐慌,老刑警劉巖正卧,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蠢熄,死亡現(xiàn)場離奇詭異,居然都是意外死亡炉旷,警方通過查閱死者的電腦和手機护赊,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進(jìn)店門惠遏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來砾跃,“玉大人骏啰,你說我怎么就攤上這事〕楦撸” “怎么了判耕?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長翘骂。 經(jīng)常有香客問我壁熄,道長,這世上最難降的妖魔是什么碳竟? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任草丧,我火速辦了婚禮,結(jié)果婚禮上莹桅,老公的妹妹穿的比我還像新娘昌执。我一直安慰自己,他們只是感情好诈泼,可當(dāng)我...
    茶點故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布懂拾。 她就那樣靜靜地躺著,像睡著了一般铐达。 火紅的嫁衣襯著肌膚如雪岖赋。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天瓮孙,我揣著相機與錄音唐断,去河邊找鬼。 笑死杭抠,一個胖子當(dāng)著我的面吹牛脸甘,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播祈争,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼斤程,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了菩混?” 一聲冷哼從身側(cè)響起忿墅,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎沮峡,沒想到半個月后疚脐,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡邢疙,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年棍弄,在試婚紗的時候發(fā)現(xiàn)自己被綠了望薄。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,561評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡呼畸,死狀恐怖痕支,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蛮原,我是刑警寧澤卧须,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站儒陨,受9級特大地震影響花嘶,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蹦漠,卻給世界環(huán)境...
    茶點故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一椭员、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧笛园,春花似錦隘击、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至蚜印,卻和暖如春莺禁,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背窄赋。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工哟冬, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人忆绰。 一個月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓浩峡,卻偏偏與公主長得像,于是被迫代替她去往敵國和親错敢。 傳聞我的和親對象是個殘疾皇子翰灾,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,573評論 2 359

推薦閱讀更多精彩內(nèi)容