0

私は配列を持っています。しかし、whileループにあるときにすべての値を出力しているわけではありません。クレイジーなキャラクターを与えています。何か案は。

    int x = 0;
    char a[3][20];


     strcpy(a[0], "Tires");
     strcpy(a[1], "Lights");
     strcpy(a[2], "Seats");

   while(statement here)
   {

      for(x = 0; x< 3; x++)
      {
         printf("%c type", a[x]);
      }
   }
4

5 に答える 5

3

printf は次のようになります。

printf("%s type\n", a[x]);

配列の要素は文字列であるためです。

printf出力の上のようにステートメントを変更した後:

出力:

Tires type
Lights type
Seats type

必要に応じて、追加\nした のを削除できます。printfその場合の出力は次のとおりです。

Tires typeLights typeSeats type

これが私のコードです:(タイヤはこのインプリメンテーションで表示されるはずです)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
 int x = 0;
 char a[3][20];


 strcpy(a[0], "Tires");
 strcpy(a[1], "Lights");
 strcpy(a[2], "Seats");

 while(1) // i left in this while as may be using it for something that you haven't shown in your code. 
 {        // But if you are not using while get rid of it .. its unnecessary 

   for(x = 0; x< 3; x++)
   {
      printf("%s type\n", a[x]);
   }
   break;
}

return 0;

}

このコードの実行方法は次のとおりです。

Notra:Desktop Sukhvir$ gcc -Werror -Wall -g -o try try.c -std=c99
Notra:Desktop Sukhvir$ ./try
Tires type
Lights type
Seats type
于 2013-10-19T03:28:26.097 に答える
1

文字列を印刷するためのものであることに printf("%s type", a[x]); 注意してください。%s

于 2013-10-19T03:26:10.920 に答える
1

%cは単一文字のフォーマット文字列ですが、文字の配列、つまり文字列へのポインターを渡しています。使用%s:

printf("%s type\n", a[x]);

プログラムをそのまま使用すると、フォーマット文字列と引数が一致しないため、未定義の動作が発生します。

于 2013-10-19T03:26:43.487 に答える
0
int x = 0;
    char a[3][20];


     strcpy(a[0], "Tires");
     strcpy(a[1], "Lights");
     strcpy(a[2], "Seats");

   while(statement here)
   {

      for(x = 0; x< 3; x++)
      {
         printf("%s type", a[x]);
      }
   }
于 2013-10-19T03:29:01.773 に答える
0

その正常に動作します。

[0] エントリも表示されています。

ここに画像の説明を入力

于 2013-10-19T03:37:36.557 に答える