#6
by みけCAT » 8年前
box さんが書きました:そもそも
yg さんが書きました:
コード:
for (int i = 0; i <= 19; i++)memo[i] = NULL;
memo[]を初期化しようとしているのなら、不要です。
このプログラムでは不要ではありません。
box さんが書きました:どうせfscanf()の呼び出しで初期化の結果を上書きしますので。
fscanfのエラーチェックをせず、現に-1が返ってきているということは読み込めず上書きしていないということなので、
printfで未初期化の自動変数の値を使って未定義動作を出さないために初期化は必要です。
box さんが書きました:それに、char型に、(void *)型のはずのNULLを放り込んでいるのは謎です。
それはそうですね。
for文を使わずに、
でいいでしょう。
ただし、C言語ではNULLはvoid *型とは限りません。
N1570 6.3.2.3 Pointersより引用
3 An integer constant expression with the value 0, or such an expression cast to type
void *, is called a null pointer constant. 66)
66) The macro NULL is defined in <stddef.h> (and other headers) as a null pointer constant; see 7.19.
N1570 7.19 Common definitions <stddef.h>より引用
NULL
which expands to an implementation-defined null pointer constant
NULLは値0の整数の可能性があり、void *型とは限りません。
[quote="box" id=3,19216,145556]そもそも
[quote="yg" id=3,19216,145552]
[code=C]
for (int i = 0; i <= 19; i++)memo[i] = NULL;
[/code][/quote]
memo[]を初期化しようとしているのなら、不要です。[/quote]
このプログラムでは不要ではありません。
[quote="box" id=3,19216,145556]どうせfscanf()の呼び出しで初期化の結果を上書きしますので。[/quote]
fscanfのエラーチェックをせず、現に-1が返ってきているということは読み込めず上書きしていないということなので、
printfで未初期化の自動変数の値を使って未定義動作を出さないために初期化は必要です。
[quote="box" id=3,19216,145556]それに、char型に、(void *)型のはずのNULLを放り込んでいるのは謎です。[/quote]
それはそうですね。
for文を使わずに、
[code=c]char memo[20] = "";[/code]でいいでしょう。
ただし、C言語ではNULLはvoid *型とは限りません。
[url=http://chimera.roma1.infn.it/SP/COMMON/iso-iec-9899-1990.pdf]N1570[/url] 6.3.2.3 Pointersより引用
[quote]
3 An integer constant expression with the value 0, or such an expression cast to type
void *, is called a null pointer constant. 66)
[/quote][quote]
66) The macro NULL is defined in <stddef.h> (and other headers) as a null pointer constant; see 7.19.
[/quote]
N1570 7.19 Common definitions <stddef.h>より引用
[quote]
NULL
which expands to an implementation-defined null pointer constant
[/quote]
NULLは値0の整数の可能性があり、void *型とは限りません。