2

我想知道是否有人可以帮助我解决以下代码片段。我想弄清楚的是如何将一个数组存储在另一个数组中。我已经尝试了所有我能想到的方法,但都导致编译器出错。以下是我的代码片段,应该足以向您展示我的立场:

char min[20], max[20], input[20] ;
printf("Enter word: ");
scanf("%s", &input);
min = input; max = input;
4

4 回答 4

3
char min[20], max[20], input[20] ;
printf("Enter word: ");
scanf("%s", input);
strcpy(min, input);
strcpy(max, input);

这就是你的做法。请注意,我已经删除&scanf也。

scanf不是一个很好的功能 - http://c-faq.com/stdio/scanfprobs.html

#include <string.h>获取strcpy.

于 2013-04-15T04:44:25.227 回答
2

我认为您必须将输入复制到max数组min中。所以代码应该是

char min[20], max[20], input[20] ;
printf("Enter word: ");
scanf("%s", input);
strcpy(min,input);
strcpy(max,input);
于 2013-04-15T04:48:16.760 回答
1

memcpy是你的朋友:

char min[20], max[20], input[20];
memset(min,'d',19);
min[19] = 0;
memcpy(min,max,20);
于 2013-04-15T04:45:01.543 回答
0

您应该尝试复制字符串。

strncpy(input, min, sizeof(min)-1);
strncpy(input, max, sizeof(max)-1);
//to be careful
min[sizeof(min)-1] = '\0';
max[sizeof(max)-1] = '\0';
于 2013-04-15T04:45:17.180 回答