プログラミングのエラーコード解説

このQ&Aのポイント
  • プログラミングにおいてかけ算を利用したプログラムでエラーコードが表示される問題が発生しています。
  • MacBook Proでのプログラム実行でエラーメッセージが発生しており、デバッグの方法がわかりません。
  • format関数の引数の指定にエラーがあり、warningメッセージが表示されています。
回答を見る
  • ベストアンサー

プログラミングについてです。

単純なかけ算を利用したプログラムなんですが、エラーコードが出てきてしまって、全然デバックできません。 わかる方、どこがどういうふうに間違っているか、ご教授お願いします。 使っているPCはMacBook Proです。 #include <stdio.h> int main( void ) { int go, hachi, goukei, azukari, otsuri ; printf("五十円切手の枚数と八十円切手の枚数を入力してください。(例 10,4):") ; scanf("%d,%d", go, hachi) ; goukei = go * 50 + hachi * 80 ; printf("合計金額は%dです、いくら支払いますか?:",goukei) ; scanf("%d 円",azukari) ; otsuri = azukari - goukei ; printf("ありがとうございました、おつりは %d 円です。",otsuri) ; return 0 ; } エラー内容 q2_5.c: In function ‘main’: q2_5.c:8: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’ q2_5.c:8: warning: format ‘%d’ expects type ‘int *’, but argument 3 has type ‘int’ q2_5.c:8: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’ q2_5.c:8: warning: format ‘%d’ expects type ‘int *’, but argument 3 has type ‘int’ q2_5.c:11: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’ q2_5.c:11: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’ です。お願いします。

質問者が選んだベストアンサー

  • ベストアンサー
noname#208507
noname#208507
回答No.1

scanfの二番目以降の引数には,変数のポインタを指定します. 単に変数名を書くとその変数の値を渡してしまうので,変数名の前に&記号を付ける必要があります. 具体的には,警告の出ている8行目,11行目を次のように直せばOKです. scanf("%d,%d", &go, &hachi) ; scanf("%d 円", &azukari) ;

参考URL:
http://linuxjm.sourceforge.jp/html/LDP_man-pages/man3/scanf.3.html
skatestoke
質問者

お礼

早速のご返答ありがとうございます! こんな単純なミスを犯すなんて、、、、ショックです。。 とても助かりました、ありがとうございます!

関連するQ&A

  • (Mac) sizeof演算子のコンパイル

    現在MacBookAirにて入門書を参考にC言語を学んでいます。 その中で、 #include <stdio.h> int main(void) { int a = 1; printf("short int型のサイズは%dバイトです。\n", sizeof(short int)); printf("変数aのサイズは%dバイトです。\n", sizeof(a)); return 0; } と入力しコンパイルすると sample21.c:7: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int’ sample21.c:8: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int’ と表示されコンパイルすることができません。 なんども見直してコードに間違いはないと思いますが、原因が解りません。 他のコードは問題なくコンパイルできます。 この先、sizeof演算子を使って値を求めなければならない章があり、非常に困っています。 どなたか解決策を教えてください。お願いします。

  • macでのポインタの扱いについて

    最近macを使ってc言語の勉強をしています。 ポインタの所でどうしてもつまづいて解決できないことがあるため質問させていただきました。 ある変数のアドレスを表示するのに main(){ int d=5; printf("dのアドレス:%d 値:%d\n",&d,d); return 0; } のようなプログラムを書いたときにMacでコンパイルすると なぜか以下のようなエラーがでてしまいます $ gcc test.c -o test.out test.c: In function ‘main’: test.c:8: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’ まったく同じソースをWindows上でBorlandを使ってコンパイルしてみると 問題ありません。 Mac環境とWindows環境ではポインタ指定の表記が違うのかと思ったのですが 調べてみてもよくわかりません。 もしわかる方がいらっしゃいましたらご教授願えないでしょうか?

  • NSString文字列をprintfで出力するには

    Objective-CでNSStringクラスの文字列をprintf関数で出力しようとするとコンパイルエラーになります。 どうすればprintfで出力できるようになるでしょうか。 実行環境はMacOSX。Xcodeをインストールしています。 ソースコードとエラーメッセージは以下です。 #import <Foundation/NSObject.h> #import <stdio.h> int main(void){ NSString *str01 = @"test"; printf("%s\n", [str01 UTF8String]); return 0; } <コンパイルエラーメッセージ> warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘id’

  • X11に関して質問です。ウィンドウIDの表示。

    /* ウィンドウIDを表示 */ printf("%d\n", w); の部分でウィンドウID(XCreateSimpleWindow関数が返す値)を表示したいのですがエラーが出ます。 表示するにはどうすればよいでしょうか? #include <X11/Xlib.h> #include <X11/Xutil.h> #include <stdio.h> #include <stdlib.h> int main() { Display *display; Window w; /* ディスプレイ・サーバに接続 */ display = XOpenDisplay(NULL); if(display == NULL){ fprintf(stderr, "Cannot open display.\n"); exit(1); } /* ウィンドウを生成 */ w = XCreateSimpleWindow(display, DefaultRootWindow(display), 50, 50, 400, 300, 1, BlackPixel(display, 0), WhitePixel(display, 0)); /* ウィンドウIDを表示 */ printf("%d\n", w); return 0; } ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー エラー内容 ウィンドウ生成要求.c: In function ‘main’: ウィンドウ生成要求.c:24:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘Window’ [-Wformat=] printf("%d\n", w);

  • 配列のアドレス取得が出来ません(in MacOSX 10.6)。

    配列のアドレス取得が出来ません(in MacOSX 10.6)。 例えば、配列要素数3の一次元配列で、int hei[3] = {x, y, z};(xyzは整数)で初期化した配列 hei の、それぞれの要素のアドレスを取得しようと思い、 %x を使って、for 文で {printf("address: %x\n", &hei[i])} としてターミナルのコマンドからgccでコンパイルすると、"warning: format ‘%x’ expects type ‘unsigned int’, but argument 2 has type ‘int *’" とエラーが出てきてしまいます(配列が char 型でも double 型でも表示は同じです。'%x' は 'unsigned int' だから、と跳ねられます)。 これはどういう理由によりますでしょうか?ちなみに使っている教科書は、1993年が初版で2000年に増補改訂された「入門 ANSI-C」という実教から出ているものです。簡単な質問で申し訳ありませんがよろしくお願いします。

  • 関数への構造体の配列の渡し方<c言語初心者>

    こんにちは、関数への構造体の配列の渡し方で理解できない点があるため、質問させていただきます。 以下がスクリプトになります。3人の名前と年齢をinput関数で入力し、それらのデータをoutput関数で出力するのが目的です。 #include <stdio.h> typedef struct{ char name[64]; int age; }property; void input(property *data[]); void output(property *data[]); int main(void){ property data[3]; printf("Input data of three people.\n"); input(&data); output(&data); return 0; } void input(property *data[]){ int i; for(i=0;i<3;i++){ printf("%d banme\n",i+1); printf("name:"); scanf("%s",&data[i]->name); printf(" age:"); scanf("%3d",&data[i]->age); } return; } void output(property *data[]){ int i; for(i=0;i<3;i++){ printf("%d banme\n",i+1); printf("name:%s\n",data[i]->name); printf("age :%3d\n",data[i]->age); } return; } コンパイル時のエラーメッセージは以下のようになりました。(ファイル名はstructure5.c) structure5.c: In function ‘main’: structure5.c:14:2: warning: passing argument 1 of ‘input’ from incompatible pointer type [enabled by default] input(&data); ^ structure5.c:8:6: note: expected ‘struct property **’ but argument is of type ‘struct property (*)[3]’ void input(property *data[]); ^ structure5.c:15:2: warning: passing argument 1 of ‘output’ from incompatible pointer type [enabled by default] output(&data); ^ structure5.c:9:6: note: expected ‘struct property **’ but argument is of type ‘struct property (*)[3]’ void output(property *data[]); ^ structure5.c: In function ‘input’: structure5.c:24:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[64]’ [-Wformat=] scanf("%s",&data[i]->name); ^ 構造体の配列をinput関数やoutput関数に渡すときにエラーが発生しているようなのですが、自分で調べても解決できなかったため、質問させて頂きます。 皆様のお知恵を貸してください。なおプログラミング言語自体初心者のため、できる限りわかりやすいお言葉でご教授願います。よろしくお願い致します。

  • 【C言語】 コンパイルエラーが解決できません

    参考書でC言語学習中の初心者です。 参考書記載のサンプルコードをコンパイルするとエラーになります。 誤字脱字は何度も確認しています。google等でも調べましたがわかりませんでした。。。 どなたか、どこが間違っているのかご教示いただけませんでしょうか。 また、何か的外れな記載があればご指摘いただけますでしょうか。 【環境】 OS: Mac OS X 10.9.3 コンパイラ:Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) 【サンプルコード】  ------------------------------------------------- #include<stdio.h> int main(void){ int data; data = 10; printf("変数dataの値 = %d\n", data); printf("変数dataのアドレス = %08X", &data); return 0; } ------------------------------------------------- 【エラー】 破線内 ------------------------------------------------- sample12-1.c:9:45: warning: format specifies type 'unsigned int' but the argument has type 'int *' [-Wformat] printf("変数dataのアドレス = %08X", &data);                     ~~~~ ^~~~~ 1 warning generated. ------------------------------------------------- 宜しくお願い申し上げます。

  • C言語のプログラムについてです

    5人の名前と身長を入力し、1番高い人の名前と身長を表示せよというものです。 下記のソースプログラムをコンパイルすると warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[10]’ と表示されます。どこを修正すればよいのでしょうか? またこのプログラムにはchar関数を使っていますが、char関数というのも良く分かりません その解説も付け加えていただけると幸いです#include <stdio.h> #include <string.h> int main(void) { int i,sincho,max_sincho; char max_namae[10],namae[10]; max_sincho=0; for(i=1;i<=5;i++){ printf("名前: "); scanf("%s", &namae); printf("身長: "); scanf("%d", &sincho); if(sincho>max_sincho){ max_sincho=sincho; strcpy(max_namae,namae); } } printf("身長が一番高い人は%sさんで%d cmです。\n", max_namae,max_sincho); return 0; }

  • プログラミングに関して

    多数省略しておりますが今このような双六をプログラミングしているのですが位置、所持金情報が上手く更新されず、上手くいかなくて困ってます。 どうすればいいでしょうか?教えてください。お願いします。 //メイン関数 int main(void){ printf("map説明 s:start,c:chance,$:money,!:振り出しに戻る,G:goal\n"); int position=0,money=500; do{ do_turn(position,money); }while(money<<0 || position==15); if(money<<0){ printf("gameover"); } } //イベント作成 void do_event(int position,int money) { int number; number=position; switch(number) { case 1:printf("チャンスタイム\n"); game1(money); break; case 2:printf("game2"); game2(money); break; case 3: printf("100Gを手に入れた"); money += 100; break; //マップ作成 void print_map(int position) { int i; for( i = 0; i <= position + 5; i++ ) { printf("%3d|",i); if(position==i){ printf("●|"); } else if(!(position==i)){ printf("○|"); } printf(" %c\n",event_name[i]); } return ; } void do_turn(int position,int money){ int me,go,energy; double z, R15=RAND_MAX+1; srand ((unsigned int) time(NULL)*314159265); z = rand() / R15; me = (int)(z*3) + 1; print_map(position); printf("現在の所持金:%dG\n",money); wait_enter(); printf("\nエネルギーを選んでください\n"); printf("1.ウコンの力 100G 2.ユンケル 200G\n"); scanf("%d",&energy); if(energy==1){ money-=100; if(me==1 || me==3) go=1; else if(position==14) go=1; else if(me==2) go=2; position+=go; printf("%d進みます\n",go); } else if(energy==2){ money-=200; if(me==1 || me==3) go=2; else if(position==14) go=1; else if(me==2) go=1; position+=go; printf("%d進みます\n",go); } print_map(position); do_event(position,money); printf("%d,%d",position,money); wait_enter(); system("pause"); } //ゲームを行う関数(じゃんけんゲーム) void game1(int money){ int comp,you; double r15=RAND_MAX+1; char* s[3]; s[0]="グー";s[1]="チョキ";s[2]="パー"; srand((unsigned int) time(NULL)*314159265); printf("じゃんけんをします勝てばお金が1.5倍に、負ければ半額を頂きます\n"); while(1){ comp=(int)(rand()/r15*3)+1; printf("1:グー、2:チョキ、3:パー ?..."); scanf("%d",&you); printf("あなたは%s,わたしは%s\n",s[you-1],s[comp-1]); if(you==comp)printf("あいこ"); else if((you+3-comp)%3==2){ printf("おめでとう、あなたの勝ち。お金が1.5倍になります\n"); money=money+money/2; break; } else{ printf("残念。あなたの負けです。お金は頂きます\n"); money=money/2; break; } } printf("所持金%dG\n",money); }

  • プログラミング教えてください!!!お願いします。

    プログラミング教えてください!!!お願いします。 次の文が実行されると何がどのようにプリントされるか。何もプリントされない時は「なし」と記せ。 また、途中に「ブランク」が入る場合は、”b”と記せ。 (1) int func1(), func2(); int data = 100; main() { int w = 1; static x =10; printf("** %d, %d, %d\n" ,w,x,data); func1(); printf("** %d, %d, %d\n" ,w,x,data); } int func1(){ int w = 2; static int x = 20: printf("*** %d, %d, %d\n", w, x, data); x += 10; func2(); printf("*** %d, %d, %d\n" , w,x,data); x *= 2; data = data - x; } int func2() { int w = 3; static int x = 30; printf("**** %d, %d, %d\n" ,w,x,data); data -= x; } (2) int func1(char *, char *, char *); int func2(char *, char *, char *); main() { char sta[20], stb[20], stc[20], std[20]; int i=0; func1("abc","xyz",sta); printf("%d -- %s\n" ,++i,sta); func1("123","456",stb); printf("%d -- %s\n" ,++i,stb); func1(sta,stb,stc); printf("%d -- %s\n" ,++i,stc); func2(sta,stb,std); printf("%d -- %s\n" ,++i,std); } int func1(char *a, char *b, char *c){ while(*a) *c++ = *a++; while(*b) *c++ = *b++; * c = 0x00; } int func2(char *a, char *b, char *c){ int i = 0; while(*b){ if(i%2 == 0) *c++ = *a++; else *c++ = *b++; i++; } *c = 0x00; }

専門家に質問してみよう