error: initialization of 'char' from 'char *' makes integer from pointer without a cast の原因と直し方
1文字を表すchar型の変数に、ダブルクォートで囲んだ文字列リテラルを代入しようとしたときに出るエラーです。シングルクォートとダブルクォートは別物です。
エラーメッセージの読み方
main.c:4:18: error: initialization of 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
main.c- ファイル名
4- 行番号
18- 列番号 — この位置でgccが構文解析に失敗しました
initialization of 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]- 内容 — 期待していたもの、または受け付けられなかったもの
このエラーが出る典型パターン
パターン1
1 #include <stdio.h> 2 3 int main(void) { 4 char grade = "A"; ^ 5 printf("%c\n", grade); 6 return 0; 7 }
main.c:4:18: error: initialization of 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
"A"はA1文字とその後の終端文字からなる文字列(char*型)です。1文字だけを表すchar型に代入するにはシングルクォートの'A'を使います。
直し方: "A" を 'A' にします。
広告
広告スロット(未設定)
パターン2
1 #include <stdio.h> 2 3 int main(void) { 4 char initial = "K"; ^ 5 printf("%c\n", initial); 6 return 0; 7 }
main.c:4:20: error: initialization of 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
ダブルクォートの"K"はポインタ型なので、char型の変数にはそのまま代入できません。
直し方: "K" を 'K' にします。
パターン3
1 #include <stdio.h> 2 3 int main(void) { 4 char sign = "+"; ^ 5 printf("%c\n", sign); 6 return 0; 7 }
main.c:4:17: error: initialization of 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
記号1文字であっても、ダブルクォートで囲むと文字列(ポインタ)になります。char型にはシングルクォートを使います。
直し方: "+" を '+' にします。
よくある誤解
'x'と"x"は見た目が近いですが、'x'は1文字を表す整数値(char型)、"x"は'x'と'\0'の2文字からなる配列へのポインタ(char*型)です。この違いがそのまま代入先の型の不一致になります。
まとめ
error: initialization of 'char' from 'char *' makes integer from pointer without a castは初級でつまずきやすい項目です。上の3パターンを実際に手で直すと、エラーメッセージのどこを読めばよいかが掴めます。