2017-12-07 6 views
-5

私はちょうどK & Rを読み始めましたし、ページ32-33に、私はほぼ完全にコピー&ペーストK&R、最長の行、同じコードを見つけるが動作しない?

finds the longest line among the inputs.

という本の中で与えられたコードだけにいくつかのコメント行を追加したコードがありますコードをわかりやすくしてください。しかし、それは動作していません。

を編集してください。悪い質問があれば申し訳ありません。 Ctrl + Zを押すとプログラムが正しく動作しないと思われます。何行入力して何回Ctrl + Zを押しても何もしません。

次はコードの私のバージョンです:事前に

/* Find the longest line among the giving inputs and print it */ 

#include <stdio.h> 
#define MAXLINE 1000   /* maximum input line length */ 

int getLine(char line[], int maxLine); 
void copy(char to[], char from[]); 

int main(void) { 
    int len; /* current line length */ 
    int max; /* maximum length seen so far */ 
    char line[MAXLINE]; /* current input line */ 
    char longest[MAXLINE]; /* longest line saved here*/ 

    max = 0; 

    /* getLine function takes all the input from user, returns it's size and equates it to the variable len 
    * Then, len is compared whether it's greater than zero because if there's no input, no need to do any calculation 
    * EDGE CASE 
    */ 
    while ((len = getLine(line, MAXLINE)) > 0) 
     /* If the length of input is larger than the previous max length, set max as the new length value and copy that input */ 
     if (len > max) { 
      max = len; 
      copy(longest, line); 
     } 
    if (max > 0) /* there was a line, EDGE CASE */ 
     printf("%s", longest); 

    return 0; 
} 

/* Read a line into s, return length. 
* Since the input length is unknown, there should be a limit */ 
int getLine(char s[], int lim) { 
    int c, i; 

    /* The loop's first condition is whether the input length is below the limit. EDGE CASE 
    * If it's not, omit the rest because it would cause a BUFFER OVERFLOW. Next, take the input as long as it's not an EOF command. 
    * Finally, if the input is end of line, finish the loop, don' take it. 
    */ 
    for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++) 
     s[i] = c; 
    if (c == '\n') 
     s[i++] = c; 
    s[i++] = '\0'; // always put a '\0' character to a string array ending, so that the compiler knows it's a string. 
    return i; 
} 

void copy(char to[], char from[]) { 
    int i = 0; 

    // This loop is readily assigns all chars from the source array to the target array until it reaches the ending char. 
    while ((to[i] = from[i]) != '\0') 
     ++i; 
} 

ありがとう!

+0

質問がありますか? – acraig5075

+0

@ acraig5075問題は、「ユーザーが入力した行の中で最も長い行を見つける」ことです。私の問題は、ちょうど少数のコメントで本に書かれている同じコードをコピーして貼り付けたのですが、うまくいきません。 –

+1

誰かがあなたに問題を持ってきて、「どういうわけかどういうわけではない」と言ったら、どうしますか? :/ – unwind

答えて

3

オーケーは、ここにエラーがあります:

s[i++] = '\0'; // always put a '\0' character to a string array ending, so that the compiler knows it's a string. 

これは(それが直接EOFを得たときに)それも無入力のための文字列を終了させます、そして、それはそれを返す前にiをインクリメントするので、getLine()が戻ることはありません0、したがってmain()は決して停止しません。私の簡単なテストのために++を削除しました。

また、コメントは誤解を招くものであり、コンパイラは何も知らない。コンパイラは、コードが実行されているときはもはや周囲にはありません。文字列のインメモリ形式は、それが期待しているので実行時ライブラリを幸せにするために必要なものです。

+0

これはつまり、私は落ち着いていない状態で起きていないバグを作成することができたことを意味します。DIがあなたのポイントを得ました。 –

関連する問題