2

以下のprintfを最初に呼び出すと、私の行が出力されます。

printfの2番目の呼び出しは、空白を出力します。

どうしたの?

while (getline(&line, &size, config_file) != -1) {

    printf("line: %s\n", line);

    /* check to see if this line is a comment */
    char marker[1];
    sscanf(line, "%1s", marker);

    printf("line: %s\n", line); 

    if (marker[0] == '#') {
    .
    .
4

1 に答える 1

4
sscanf(line, "%1s", marker);

指定した1文字とは別に、\0ターミネータもに格納されmarkerます。明らかに未定義ですが、私の推測では、を\0上書きしますline

つまりmarker、1文字の文字列を格納するには、幅が2文字以上である必要があります。

于 2013-03-05T19:13:58.047 に答える