读取n字符串并保存在字符串数组中 [英] Read n string and save in string array
本文介绍了读取n字符串并保存在字符串数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Read_str(char a[][10],int b);
void Print_str(char a[][10],int b);
int main(void) {
int b1;
printf("insert number of string: ");
fflush(stdout);
scanf("%d",&b1);
char a1[b1][10];
Read_str(a1,b1);
Print_str(a1,b1);
return EXIT_SUCCESS;
}
void Read_str(char a[][10],int b){
int i;
for(i=0;i<b;i++)
{
printf("insert string%d",i+1);
fflush(stdout);
scanf("%s",&a[i][10]);
}
}
void Print_str(char a[][10],int b){
int i;
for(i=0;i<b;i++)
{
printf("string number%d= %s\n",i+1,a[i]);
fflush(stdout);
}
}
这是我写的所有鳕鱼。一些字符串并打印出来。
请检查此代码。当我写这个
this all of cod that i wrote.get some string and print them.
please check this code.when i write this
scanfscanf("%s",&a1[i-1][10]);
代码工作正常。但是当我写这个
the code work good.but when i write this
scanf("%s",&a1[i][10]);
代码不能正常工作。例如对于b1 == 3代码不起作用。
the code did not work good.for example for b1==3 the code did not work .
推荐答案
所有,你的索引是错误的,因为数组从C中的索引0开始。因此i-1
应该是i
。
然后& a [i-1]
与的地址不同& a [i-1] [10]
但地址与& a [i-1] [0]
相同。
顺便说一下i-1
的任何表达式在i $时无效c $ c>是0。
First of all, your indexing is wrong as array starts at index 0 in C. Thusi-1
should really bei
.
Then&a[i-1]
is not the same address as&a[i-1][10]
but the same address as&a[i-1][0]
.
By the way any expression withi-1
is not valid wheni
is 0.
这篇关于读取n字符串并保存在字符串数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文