C代码没有生成所需的结果。 [英] C code is not generating required results.
问题描述
嗨!有人可以帮我告诉我为什么下面的代码在生成第一个学生的名字时给出了垃圾价值,而它为其他三个学生正确地给出了名字
- 我真的不明白,因为
它没有产生所需的结果.-谢谢你的预期 -
Vasudev。
#包括< stdio.h>
main()
{
char学生[4] [30],* x [4] [ 30];
int i,j,k;
printf("学生的名字!\ n");
for(i = 0; i< = 3; i ++)
{
得到(student [i]);
x [i] [ 30] =& student [i] [30];
}
printf(" \ n您输入了以下名称。\ n");
for(i = 0; i< = 3; i ++)
{
puts(* x [i]);
}
printf(&\\你想要重新发布多少名字(选择1到
4)?? \ n");
scanf("%d",& k);
k = k -1;
if(k> = 0)
{
for(j = 0; j< = k; j ++)
{
printf(你想知道哪个学生的名字,第1,第2,第3或
第4个?\ n");
scanf("%d",& i);
i = i-1;
printf("学生姓名=%s \ n",* x [i]);
}
}
}
Hi! Can somebody help tell me why the following code gives a garbage
value while producing first student''s name, whereas it gives the names
correctly for other three students - I really fail to understand since
it is not generating the required results.- Thank you in anticipation -
Vasudev.
#include <stdio.h>
main ()
{
char student[4][30], *x[4][30];
int i, j,k;
printf("Student''s name!\n");
for(i=0;i<=3;i++)
{
gets(student[i]);
x[i][30]=&student[i][30];
}
printf("\nYou have typed the following names.\n");
for(i=0;i<=3;i++)
{
puts(*x[i]);
}
printf("\nHow many names do you want to republish (choice 1 to
4)??\n");
scanf("%d",&k);
k=k-1;
if(k>=0)
{
for(j=0;j<=k;j++)
{
printf("Which Student''s name do you want to know, 1st, 2nd, 3rd or
4th??\n");
scanf("%d", &i);
i=i-1;
printf("Student''s name = %s\n", *x[i]);
}
}
}
推荐答案
尝试下面的...现在正在运作....你没有填充
x数组正确...
#include< stdio.h>
main()
{
char学生[4] [30],* x [4] [30];
int i,j,k;
printf(" Student'的名字!\ n");
for(i = 0; i< = 3; i ++)
{
//得到(学生[i]);
scanf("%s",student [i]);
printf("%s",student [i] );
* x [i] =学生[i];
}
printf(" \ n你输入了e以下姓名。\ n");
for(i = 0; i< = 3; i ++)
{
puts( * x [i]);
}
printf(&\\你想要重新发布多少名字(选择1到
)
4)?? \ n");
scanf("%d"& k);
k = k-1;
if(k> = 0)
{
for(j = 0; j< = k; j ++)
{
printf(你想知道哪个学生的名字,第1,第2,第3或
4th ?? \ n);
scanf("%d",& i);
i = i-1;
printf(" Student's name) =%s \ n",* x [i]);
}
}
}
Try the below one... It is working now.... You were not populating the
x array properly...
#include <stdio.h>
main ()
{
char student[4][30], *x[4][30];
int i, j,k;
printf("Student''s name!\n");
for(i=0;i<=3;i++)
{
//gets(student[i]);
scanf("%s",student[i]);
printf("%s", student[i]);
*x[i]=student[i];
}
printf("\nYou have typed the following names.\n");
for(i=0;i<=3;i++)
{
puts(*x[i]);
}
printf("\nHow many names do you want to republish (choice 1 to
4)??\n");
scanf("%d",&k);
k=k-1;
if(k>=0)
{
for(j=0;j<=k;j++)
{
printf("Which Student''s name do you want to know, 1st, 2nd, 3rd or
4th??\n");
scanf("%d", &i);
i=i-1;
printf("Student''s name = %s\n", *x[i]);
}
}
}
嗨亲爱的!
非常感谢您回复邮件所遇到的麻烦。我ñ
尝试了你的代码,不幸的是这也是错误的。你知道吗,
输入名字时,我们的名字和名字之间有一个空格
介于两者之间,这就是为什么我用''获得''分配的原因
指针的名字很好,除了名字。现在在你的代码中我发现
''// gets()。我不知道这些双斜线是什么以及为什么你在get()之后使用了scanf和printf语句
。但是,这段代码是
不接受名字/姓氏之间的空格而且总是给b
$ b给出错误。
但是,再次感谢你的帮助,但如果你能回复我的上述询问,我将不胜感激。
Vasudev
ma*********@gmail.com 写道:
尝试下面的...现在正在运行....你没有正确填充
x数组......
#include< stdio.h>
main()
{char student [4] [30],* x [4] [30];
int i,j,k;
printf (学生的名字!\ n);
for(i = 0; i< = 3; i ++)
{得到(学生[i]) ;
scanf("%s",student [i]);
printf("%s",student [i]);
* x [i] = student [i ];
}
printf(&\\;你输入了以下名字。\ n");
for(i = 0; i< = 3; i ++)
{
put(* x [i ]);
}
printf(&\\你想要重新发布多少名字(选择1到
4)?? \ n );
scanf("%d"& k);
k = k-1;
if(k> = 0)
(j = 0; j <= k; j ++)
{/> printf(你想知道哪个学生的名字,第1,第2,第3或第4个?? \ n");
scanf("%d",& i);
i = i-1;
printf(" Student's name =%s \\ \\ n,* x [i]);
}
}
}
Try the below one... It is working now.... You were not populating the
x array properly...
#include <stdio.h>
main ()
{
char student[4][30], *x[4][30];
int i, j,k;
printf("Student''s name!\n");
for(i=0;i<=3;i++)
{
//gets(student[i]);
scanf("%s",student[i]);
printf("%s", student[i]);
*x[i]=student[i];
}
printf("\nYou have typed the following names.\n");
for(i=0;i<=3;i++)
{
puts(*x[i]);
}
printf("\nHow many names do you want to republish (choice 1 to
4)??\n");
scanf("%d",&k);
k=k-1;
if(k>=0)
{
for(j=0;j<=k;j++)
{
printf("Which Student''s name do you want to know, 1st, 2nd, 3rd or
4th??\n");
scanf("%d", &i);
i=i-1;
printf("Student''s name = %s\n", *x[i]);
}
}
}
va ************** @ yahoo .co.uk 写道:
va**************@yahoo.co.uk wrote:
#include< stdio.h>
main()
int main(无效)
{
char student [4] [30],* x [4] [30];
int i,j,k;
printf(学生的名字!\ n);
for(i = 0; i< = 3; i ++)
{
得到(student [i]);
永远不要使用gets()。永远。你的选择包括fgets()和scanf()。
x [i] [30] =& student [i] [30];
#include <stdio.h>
main ()
int main( void )
{
char student[4][30], *x[4][30];
int i, j,k;
printf("Student''s name!\n");
for(i=0;i<=3;i++)
{
gets(student[i]);
NEVER use gets(). Ever. Your alternatives include fgets() and scanf().
x[i][30]=&student[i][30];
你正在过度索引。 x [3] [30]不存在。您的x
阵列究竟为您完成了什么? (提示:不多。)
我还没看到你真正的错误可能在哪里。在
的未来,修复你的代码缩进。它使提供帮助变得更容易。
-
Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我
ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。
You''re overindexing. x[3][30] does not exist. What exactly is your x
array accomplishing for you? (Hint: Not much.)
I haven''t looked to see where your real error might be. In the
future, fix your code indentation. It makes offering help easier.
--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.
这篇关于C代码没有生成所需的结果。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!