「strcat」について
Posted: 2012年7月11日(水) 00:13
C言語初心者です、下記のコードにて私では理解することの出来ない部分が多々ありましたので、どなたかご指導お願いします。
上記でコメントをしている部分が理解できないため全体的に把握でいていない状態です。
int field[5][5];
void initField()
{
int i,j;
for(j = 0 ; j < 6 ; j++)
{
for(i = 0 ; i < 6 ; i++)
{
field[j][i] = 1;
}
}
for(i = 0 ; i < 6 ; i++)
{
field[i][0] = 0;
field[i][6 - 1] = 0;
field[0][i] = 0;
field[6 - 1][i] = 0;
}
}
//strcatで使用する
char *str2[6] = {"◇","1","2","3","4","△"};
//-----------------
int main(void)
{
int i,j;
initField();
for(j = 0 ; j < 6 ; j++)
{
char str1[13];
str1[0] = '\0';//-----------------------なぜこの処理をしなければならないのかわかりません
for(i = 0 ; i < 6 ; i++)
{
switch(field[j][i])
{
case 0:
if(j == 0)
strcat(str1,str2[i]);//ここの処理はstr1[]にstr2[i]番目の値を連結していくですよ
// ね?次の処理では1行目の処理で「str1[]に6個+NULL」の
// 入った 所に2行目表示の処理str1[]が入る
// これを繰り返すとchar str1[13];を溢れてしまうのですが・・・
else if(i == 0)
strcat(str1,str2[j]);
else
strcat(str1,"□");
break;
case 1:
strcat(str1,"・");
}
}
printf("%s\n",str1);
}
}