我试图为a1和a2输入字符串值(每个数组超过1)。我想把它们一行一行地打印出来但是程序不打印任何值。我怎样才能修好它?
#include<stdio.h>
int main (void){
int i;
char *a1[10], *a2[10];
for (i=0; i<10;i++){
printf("Enter text %d for a1", i);
scanf("%s",a1);
printf("Enter text %d for a2", i);
scanf("%s",a2);
}
for (i=0; i<10;i++){
printf("text %d is %s for a1",i,a1[i]);
printf("text %d is %s for a2",i,a2[i]);
}
return 0;
}
我试图打印数组的元素一行一行,但我不能。程序也不会给出错误。程序在输入元素后结束。
- 您的程序无法编译,因为您从未定义过cut。
- 使用常量代替硬编码魔法值。
a1
和a2
是指针数组。你需要为你想要读取的字符串分配空间。- 两个
scanf()
覆盖数据到相同的变量,但你希望它是相对于索引。 - 当使用
scanf()
读取字符串时,确保设置最大字符串长度以避免缓冲区溢出。考虑使用fgets()
- 检查
scanf()
的返回值,否则你的变量可能不会初始化。 - 为了可读性,在你的
printf()
中使用n
。
下面是工作代码:
#include <stdio.h>
#define N 10
#define STR_LEN 99
#define STR(s) STR2(s)
#define STR2(s) #s
int main (void){
char a1[N][STR_LEN+1];
char a2[N][STR_LEN+1];
for (int i=0; i<N; i++){
printf("Enter text %d for a1: ", i);
if(scanf("%" STR(STR_LEN) "s", &a1[i]) != 1) {
printf("scanf failedn");
return 1;
}
printf("Enter text %d for a1: ", i);
if(scanf("%" STR(STR_LEN) "s", &a2[i]) != 1) {
printf("scanf failedn");
return 1;
}
}
for (int i=0; i<N; i++) {
printf("text %d is %s for a1n",i, a1[i]);
printf("text %d is %s for a2n",i, a2[i]);
}
}
和示例:
$ seq 20 | ./a.out
Enter text 0 for a1: Enter text 0 for a2: Enter text 1 for a1: Enter text 1 for a2: Enter text 2 for a1: Enter text 2 for a2: Enter text 3 for a1: Enter text 3 for a2: Enter text 4 for a1: Enter text 4 for a2: Enter text 5 for a1: Enter text 5 for a2: Enter text 6 for a1: Enter text 6 for a2: Enter text 7 for a1: Enter text 7 for a2: Enter text 8 for a1: Enter text 8 for a2: Enter text 9 for a1: Enter text 9 for a2: text 0 is 1 for a1
text 0 is 2 for a2
text 1 is 3 for a1
text 1 is 4 for a2
text 2 is 5 for a1
text 2 is 6 for a2
text 3 is 7 for a1
text 3 is 8 for a2
text 4 is 9 for a1
text 4 is 10 for a2
text 5 is 11 for a1
text 5 is 12 for a2
text 6 is 13 for a1
text 6 is 14 for a2
text 7 is 15 for a1
text 7 is 16 for a2
text 8 is 17 for a1
text 8 is 18 for a2
text 9 is 19 for a1
text 9 is 20 for a2
这是一个重构的版本,它使用了几个函数来减少重复。也使用fgets()
而不是scanf()
,因为您可能不喜欢后者在默认情况下读取与行相反的单词:
#include <stdio.h>
#include <string.h>
#define N 10
#define STR_LEN 100
int prompt_str(int index, const char *name, size_t len, char s[len]) {
printf("Enter text %d for %s: ", index, name);
if(!fgets(s, len, stdin)) {;
return 1;
};
s[strcspn(s, "n")] = ' ';
return 0;
}
void print_str(int index, const char *name, const char *s) {
printf("text %d is %s for %sn", index, s, name);
}
int main (void){
char a1[N][STR_LEN];
char a2[N][STR_LEN];
for (int i=0; i<N; i++){
if(
prompt_str(i, "a1", STR_LEN, a1[i]) ||
prompt_str(i, "a2", STR_LEN, a2[i])
)
return 1;
}
for (int i=0; i<N; i++) {
print_str(i, "a1", a1[i]);
print_str(i, "a2", a2[i]);
}
}
这不是很清楚,但我认为你在寻找这样的东西:
#include<stdio.h>
int
main(void)
{
int i;
char a1[10][256], a2[10][256];
for( i = 0; i < 10; i += 1 ){
printf("Enter text %d for a1", i);
if( 1 != scanf("%255s", a1[i]) ){
fprintf(stderr, "Invalid inputn");
return 1;
}
printf("Enter text %d for a2", i);
if( 1 != scanf("%255s", a2[i]) ){
fprintf(stderr, "Invalid inputn");
return 1;
}
}
for( i = 0; i < 10; i += 1 ){
printf("text %d is %s for a1n", i, a1[i]);
printf("text %d is %s for a2n",i , a2[i]);
}
return 0;
}
有几件事要注意。如果您希望将变量声明为指针数组,则需要以某种方式分配空间(可能通过调用malloc
并分配指针)。使用数组的数组可能比使用指针的数组更简单。必须总是检查scanf
返回的值。您必须始终在s
转换说明符上放置宽度修饰符。在scanf
中使用原始%s
并不比gets
好。