0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

NULLと '\0' の違い

Last updated at Posted at 2021-08-12

Q. a[i] = NULL
a[i] = \0
上記のどちらが正解だろうか?

NULL'\0'もいずれも0をあらわしている。

NULL はアドレスが 0
NULL はマクロ定義 #define NULL ((void *)0)としている。

'\0' は文字コード(ASCIIコード表)が 0 である
文字の'\0':文字コード(ASCII)の番号が 0

A. a[i] = '\0' が正解である。
  a[i] = NULL これは値がポインタと数値を比較してしまっている為である。

まとめ 

0 整数の 0
NULL どこも参照していないことを表すポインター(マクロ)。
     値はアドレスの0番(値0)。
空文字('\0') ASCIIコード(文字コード)値0(NULL文字)を指す。
空文字列("") 文字'\0'の配列(文字配列)。値はこの配列の先頭アドレスとなる。

整数の0NULL,空文字('\0')は値がともに0であるため,結果としては混同して使っても問題ないことが多い。

ただし,空文字列は大きく意味が異なる。空文字列は空文字('\0')を要素として持つ要素数1の配列である。そのため,単独の空文字列の値は配列の先頭アドレスとなる。空文字列の先頭要素[0]は空文字('\0')であるため値は0である。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?