为什么C程序会给出这样的结果? [英] Why does the C program give such results?

查看:54
本文介绍了为什么C程序会给出这样的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用以下函数时,只有第二个printf会打印预期的输出,而第一个printf只打印一个字符(不一定与初始字符串相关)。但是,如果我注释掉与* s2相关的陈述,那么第一个printf给我提供了所需的输出。



例如:

char a [] =HELLO WORLD;

char b [] = 我的名字是约翰;

调用函数(a,b)会产生这样的结果:

n

我的名字是john



但是评论与* s2相关的字段将给出:

hello world



为什么会这样?谢谢。



我的尝试:



When I call the following function, only the 2nd printf will print the expected output while the first printf only print a character (not necessarily related to the initial string). However, if I comment out statements relating to *s2, quite bizarrely the first printf gives me the required output.

For instance:
char a[]="HELLO WORLD";
char b[]="MY NAME IS JOHN";
calling afunction(a,b) will produce something like this:
n
my name is john

but commenting out fields relating to *s2 will give:
hello world

Why the hell is that? Thanks.

What I have tried:

int afunction(char *s1, char *s2){
	char *a,*b,*c,*d;
	c=a;
	d=b;
	while (*s1!='\0'){
		*a++=tolower(*s1++);
	}
	*a='\0';
	
	while (*s2!='\0'){
		*b++=tolower(*s2);
		s2++;
	}
	*b='\0';	
	printf("%s\n",c);
	printf("%s\n",d);
	return 0;
}

推荐答案

这是一些有趣的代码行......你忘记了对于初学者来说有多么令人困惑的东西: - )



好​​的大提示你真的不需要a,b,c和d你已经有s1和s2,这是六个指针。你有很多指针,你已经迷失在你自己制造的剪切复杂性中。你学习编码的部分原因是将问题提炼到最低限度。



所以你的问题是函数内的前三行

That is some funny lines of code .. you forget how baffling things are for beginners :-)

Okay big tip you really don't need a, b, c and d and you already have s1 and s2, that is six pointers. You have got lost in the shear complexity all of your own making by having so many pointers. Part of your learning to code is to distill the problem down to the bare minimum.

So your problem is the first 3 lines inside the function
char *a,*b,*c,*d;   // You define 4 pointers they don't point anywhere and are just junk so far
c=a;  // here you set c to have the same junk value as a
d=b;  // here you set d to have the same junk value as b



所以其他人告诉你的是你可能想要指向a和b某处...可能s1和s2值,这回到了问题...为什么这么多指针。



我猜你只需要一个额外的指针,这是临时指针你继续前进。你将它设置为s1移动它随着你的案例转换。然后你将它设置为s2移动它,随着你去每个角色进行大小写转换。然后显示s1和s2并完成工作。我不确定你是想移动s1和s2,它是指向你实际文本缓冲区的指针而你松开了它们,你已经死在水中。


So what everyone else is telling you is you might want to point a and b somewhere ... probably s1 and s2 values and this gets back to the problem ... why so many pointers.

I am guessing you need only 1 extra pointer, which is the temporary one you move along. You set it to to s1 move it along doing your case conversion as you go. Then you set it to s2 move it along doing your case conversion on each character as you go. Then you display s1 and s2 and job done. I am not sure you want to be moving s1 and s2 that is the pointer to your actual text buffers and you loose them and you are dead in the water.


你忘了初始化 * a * b 到一个小写字母的地方。

结果取决于编译器,可以是任何地方,从正确的结果,部分结果,崩溃或分段错误。



当你不明白你的代码在做什么或为什么它做什么确实如此,答案是调试器

使用调试器查看代码正在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里显示你的代码正在做什么,你的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
You forgot to initialize *a and *b to a place to homd the lowercase strings.
The result depend on compiler and can be anywhere from correct results, partial results, to crash or segment fault.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于为什么C程序会给出这样的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆