请帮我找出代码中的错误:#include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct card { char suit[10]; char face[10]; }CARD; void fillCard(CARD card[],char b[][10],char c[][10]); void printcard(CARD card[]); void main() { CARD card[52]; char suit[3][10]={"Spades","Hearts","Clubs","Diamonds"}; char face[12][10]={"A","2","3","4","5","6","7","8","9","10", "Jack","Queen","King"}; fillCard(card,suit,face); printcard(card); } void fillCard(CARD card[],char b[][10],char c[][10]) { int i,j; for(i=0;i<52;i++) { strcpy(card[i].suit,b[i/13][10]); strcpy(card[i].face,c[i%13][10]); } } void printcard(CARD card[]) { int i; for (i=0; i<52; i++) { printf("%9s%9s\n",card[i].suit,card[i].face); } }
时间: 2023-12-10 18:39:56 浏览: 120
#include stdio.h(1).docx
代码中有以下错误:
1. 在 fillCard 函数中,使用 strcpy 函数时,应该使用 b[i/13] 和 c[i%13] 来获取字符串,而不是 b[i/13][10] 和 c[i%13][10]。因为这两个数组的第二维长度只有 10,而不是 11。
2. 在 fillCard 函数中,字符串拷贝函数 strcpy 的第一个参数应该是目标字符串,第二个参数是源字符串。所以应该把 strcpy 函数中的参数顺序颠倒。
修改后的代码如下:
```
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct card {
char suit[10];
char face[10];
} CARD;
void fillCard(CARD card[],char b[][10],char c[][10]);
void printcard(CARD card[]);
int main() {
CARD card[52];
char suit[4][10]={"Spades","Hearts","Clubs","Diamonds"};
char face[13][10]={"A","2","3","4","5","6","7","8","9","10",
"Jack","Queen","King"};
fillCard(card,suit,face);
printcard(card);
return 0;
}
void fillCard(CARD card[],char b[][10],char c[][10]) {
int i,j;
for(i=0;i<52;i++) {
strcpy(card[i].suit,b[i/13]);
strcpy(card[i].face,c[i%13]);
}
}
void printcard(CARD card[]) {
int i;
for (i=0; i<52; i++) {
printf("%9s%9s\n",card[i].suit,card[i].face);
}
}
```
阅读全文