前提・実現したいこと
C言語ポインタ書籍の勉強中にコードを写したところ、エラーが発生しましたが解決の方法がよく分かりません。
型にバイトサイズが越えてるよ。っていうエラーメッセージは読めるのですがアドバイスをお願いします。C言語は初めてでvscodeでコードランナーを使ってコンパイルしています。
発生している問題・エラーメッセージ
hello.c:48:34: error: character too large for enclosing character literal type if( (p = strrchr(command,'¥n')) != NULL){ ^ hello.c:48:34: warning: multi-character character constant [-Wmultichar] hello.c:49:18: error: character too large for enclosing character literal type *p = '¥0'; ^ hello.c:49:18: warning: multi-character character constant [-Wmultichar] 2 warnings and 2 errors generated.
該当のソースコード
C言語 書籍:ポインタが理解できない理由
1 2ソースコード 3 4 5#include <stdio.h> 6#include <string.h> 7#include <stdlib.h> 8 9int list(void){ 10 printf("func list¥n"); 11 return 1; 12}; 13 14int show(void){ 15 printf("func show¥n"); 16 return 1; 17}; 18 19int quit(void){ 20 exit(0); 21}; 22 23struct command{ 24 char *com_str; 25 int (*com_func)(void); 26}; 27 28struct command coms[] = { 29 {"ls",list},{"dir",list}, 30 {"cat",show},{"type",show}, 31 {"quit",quit},{"exit",quit}, 32 {NULL,NULL} 33}; 34 35int do_command(char *command){ 36 struct command *p; 37 for(p = coms; p->com_str != NULL; p++){ 38 if( strcmp(p->com_str,command)==0){ 39 return p->com_func(); 40 } 41 } 42 printf("command not found¥n"); 43 return 0; 44} 45 46int main(void){ 47 char command[80]; 48 char *p; 49 while(1){ 50 printf(">"); 51 fgets(command,80,stdin); 52 if( (p = strrchr(command,'¥n')) != NULL){ 53 *p = '¥0'; 54 }do_command(command); 55 }return 0; 56} 57
試したこと
一部をコメントアウトして試してみましたが、更にエラーが増えました。
hello.c:15:5: note: include the header <stdlib.h> or explicitly provide a declaration for 'exit'
と出たので#includeを追記しました。
補足情報(FW/ツールのバージョンなど)
vscode 拡張機能 c/c++ code runner Macです。 環境抜かりすみません。

回答4件
あなたの回答
tips
プレビュー